如何在jquery中获取select()的值

时间:2022-11-27 19:55:45

I have search a lot but google just return me how to get the selected value of a select element

我搜索了很多,但谷歌只返回我如何获得选择元素的选定值

$('#some_id').select(function() { 
    // how can i store the selected value in a variable.?
    alert('the selected value');
});

3 个解决方案

#1


0  

You can use change event to get selected value. In the event handler you can use $(this).val() of this.value to get the selected value.

您可以使用change事件来获取选定的值。在事件处理程序中,您可以使用this.value的$(this).val()来获取所选值。

$('#some_id').change(function(){
  // how can i store the selected value in a variable.?
  v = this.value;

  alert('the selected value' + this.value);
});

#2


0  

you can try like this:

你可以尝试这样:

 $('#select_id').change(function(){
   var sval = $('#select_id').val();
   alert('the selected value' + sval);
});

#3


0  

Use .change() event. Something like this:

使用.change()事件。像这样的东西:

var current_value = 1;//can define any value null also
$('#some_id').change(function(){
    current_value = $(this).val();//storing
    alert(current_value);
});

Note: if you want to store value in variable and use it anywhere else then you must define it globally like i did.

注意:如果您想将值存储在变量中并在其他地方使用它,那么您必须像我一样全局定义它。

DEMO

#1


0  

You can use change event to get selected value. In the event handler you can use $(this).val() of this.value to get the selected value.

您可以使用change事件来获取选定的值。在事件处理程序中,您可以使用this.value的$(this).val()来获取所选值。

$('#some_id').change(function(){
  // how can i store the selected value in a variable.?
  v = this.value;

  alert('the selected value' + this.value);
});

#2


0  

you can try like this:

你可以尝试这样:

 $('#select_id').change(function(){
   var sval = $('#select_id').val();
   alert('the selected value' + sval);
});

#3


0  

Use .change() event. Something like this:

使用.change()事件。像这样的东西:

var current_value = 1;//can define any value null also
$('#some_id').change(function(){
    current_value = $(this).val();//storing
    alert(current_value);
});

Note: if you want to store value in variable and use it anywhere else then you must define it globally like i did.

注意:如果您想将值存储在变量中并在其他地方使用它,那么您必须像我一样全局定义它。

DEMO