jQuery获取选定的选项值(不是文本,而是属性'value')

时间:2022-11-27 21:34:03

Okay, I have this code:

好的,我有这个代码:

<select name="selector" id="selector">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

And I'd like to get the value of the option selected. Example: 'Option 2' is selected, and the value of it is '2'. The '2' is the value I need to get and not 'Option 2'.

我想要选择选项的值。示例:选择“选项2”,其值为“2”。“2”是我需要的值,而不是“选项2”。

12 个解决方案

#1


224  

Use .val to get the value of the selected option. See below,

使用.val来获取所选选项的值。见下文,

$('select[name=selector]').val()

#2


96  

I just wanted to share my experience and my reputation is not enough to comment.

我只是想分享我的经验,我的名声还不足以让我发表评论。

For me,

对我来说,

$('#selectorId').val()

$(" # selectorId ').val()

returned null.

返回null。

I had to use

我不得不使用

$('#selectorId option:selected').val()

$(' # selectorId选项:选择').val()

#3


22  

$('select').change(function() {
    console.log($(this).val())
});​

jsFiddle example

jsFiddle例子

.val() will get the value.

.val()将得到这个值。

#4


8  

You need to add an id to your select. Then:
$('#selectorID').val()

您需要向您的选择添加一个id。然后:$(' # selectorID ').val()

#5


4  

you can use jquery as follows

可以使用jquery如下所示。

SCRIPT

脚本

  $('#IDOfyourdropdown').change(function(){
    alert($(this).val());
  });

FIDDLE is here

小提琴在这里

#6


2  

For a select like this

对于这样的选择

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option id="0">CHOOSE AN OPTION</option>
   <option id="127">John Doe</option>
   <option id="129" selected>Jane Doe</option>

... you can get the id this way:

…您可以通过以下方式获取id:

$('#list-name option:selected').attr('id');

Or you can use value instead, and get it the easy way...

或者你也可以用value来代替,用简单的方式去获取……

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option value="0">CHOOSE AN OPTION</option>
   <option value="127">John Doe</option>
   <option value="129" selected>Jane Doe</option>

like this:

是这样的:

$('#list-name').val();

#7


2  

One of my options didn't have a value: <option>-----</option>. I was trying to use if (!$(this).val()) { .. } but I was getting strange results. Turns out if you don't have a value attribute on your <option> element, it returns the text inside of it instead of an empty string. If you don't want val() to return anything for your option, make sure to add value="".

我的一个选项没有值:。我试图使用if (!$(this).val() {.. ..但是我得到了奇怪的结果。如果您的 <选项> 元素上没有值属性,那么它将返回文本,而不是空字符串。如果不希望val()为您的选项返回任何内容,请确保添加value="" "。

#8


2  

Try this:

试试这个:

$(document).ready(function(){
    var data= $('select').find('option:selected').val();
});

or

var data= $('select').find('option:selected').text();

#9


0  

Look at the example:

看看这个例子:

    <select class="element select small" id="account_name" name="account_name"> 

        <option value="" selected="selected">Please Select</option>           
        <option value="paypal" >Paypal</option>
        <option value="webmoney" >Webmoney</option>

    </select>

 <script type="text/javascript">

        $(document).ready(function(){ 
             $('#account_name').change(function(){
               alert($(this).val());                                                          
             });
        });
 </script>

#10


0  

$('#selectorID').val();

OR

$('select[name=selector]').val();

OR

$('.class_nam').val();

#11


0  

this code work very well for me: this return the value of selected option. $('#select_id').find('option:selected').val();

这段代码对我很有效:它返回了所选选项的值。$(' # select_id ');(选项:选择).val();

#12


0  

 $(document ).ready(function() {
    $('select[name=selectorname]').change(function() { 
     alert($(this).val());});
 });

#1


224  

Use .val to get the value of the selected option. See below,

使用.val来获取所选选项的值。见下文,

$('select[name=selector]').val()

#2


96  

I just wanted to share my experience and my reputation is not enough to comment.

我只是想分享我的经验,我的名声还不足以让我发表评论。

For me,

对我来说,

$('#selectorId').val()

$(" # selectorId ').val()

returned null.

返回null。

I had to use

我不得不使用

$('#selectorId option:selected').val()

$(' # selectorId选项:选择').val()

#3


22  

$('select').change(function() {
    console.log($(this).val())
});​

jsFiddle example

jsFiddle例子

.val() will get the value.

.val()将得到这个值。

#4


8  

You need to add an id to your select. Then:
$('#selectorID').val()

您需要向您的选择添加一个id。然后:$(' # selectorID ').val()

#5


4  

you can use jquery as follows

可以使用jquery如下所示。

SCRIPT

脚本

  $('#IDOfyourdropdown').change(function(){
    alert($(this).val());
  });

FIDDLE is here

小提琴在这里

#6


2  

For a select like this

对于这样的选择

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option id="0">CHOOSE AN OPTION</option>
   <option id="127">John Doe</option>
   <option id="129" selected>Jane Doe</option>

... you can get the id this way:

…您可以通过以下方式获取id:

$('#list-name option:selected').attr('id');

Or you can use value instead, and get it the easy way...

或者你也可以用value来代替,用简单的方式去获取……

<select class="btn btn-info pull-right" id="list-name" style="width: auto;">
   <option value="0">CHOOSE AN OPTION</option>
   <option value="127">John Doe</option>
   <option value="129" selected>Jane Doe</option>

like this:

是这样的:

$('#list-name').val();

#7


2  

One of my options didn't have a value: <option>-----</option>. I was trying to use if (!$(this).val()) { .. } but I was getting strange results. Turns out if you don't have a value attribute on your <option> element, it returns the text inside of it instead of an empty string. If you don't want val() to return anything for your option, make sure to add value="".

我的一个选项没有值:。我试图使用if (!$(this).val() {.. ..但是我得到了奇怪的结果。如果您的 <选项> 元素上没有值属性,那么它将返回文本,而不是空字符串。如果不希望val()为您的选项返回任何内容,请确保添加value="" "。

#8


2  

Try this:

试试这个:

$(document).ready(function(){
    var data= $('select').find('option:selected').val();
});

or

var data= $('select').find('option:selected').text();

#9


0  

Look at the example:

看看这个例子:

    <select class="element select small" id="account_name" name="account_name"> 

        <option value="" selected="selected">Please Select</option>           
        <option value="paypal" >Paypal</option>
        <option value="webmoney" >Webmoney</option>

    </select>

 <script type="text/javascript">

        $(document).ready(function(){ 
             $('#account_name').change(function(){
               alert($(this).val());                                                          
             });
        });
 </script>

#10


0  

$('#selectorID').val();

OR

$('select[name=selector]').val();

OR

$('.class_nam').val();

#11


0  

this code work very well for me: this return the value of selected option. $('#select_id').find('option:selected').val();

这段代码对我很有效:它返回了所选选项的值。$(' # select_id ');(选项:选择).val();

#12


0  

 $(document ).ready(function() {
    $('select[name=selectorname]').change(function() { 
     alert($(this).val());});
 });