在 jQuery 中获取下拉列表的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8978328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the value of a dropdown in jQuery
提问by Nate Pet
I have a drop down that has an 'ID, Name' Pair.
我有一个下拉菜单,上面有一个“ID,名称”对。
Example
例子
Jon Miller
Jim Smith
Jen Morsin
乔恩·米勒
Jim Smith
Jen Morsin
Jon MIller has ID of 101
Jim Smith has ID of 102
Jen Morsin has ID of 103
Jon MIller 的 ID 为 101
Jim Smith 的 ID 为 102
Jen Morsin 的 ID 为 103
When I do the followng:
当我执行以下操作时:
var arNames = $('#Crd').val()
and I select Jon Miller, I get 101. I'd like to get Jon Miller though.
我选择 Jon Miller,我得到 101。不过我想要 Jon Miller。
回答by ShankarSangoli
$('#Crd').val()
will give you the selected value of the drop down element. Use this to get the selected options text.
$('#Crd').val()
将为您提供下拉元素的选定值。使用它来获取选定的选项文本。
$('#Crd option:selected').text();
回答by Sandeep Pal
The best way is to use:
最好的方法是使用:
$("#yourid option:selected").text();
Depending on the requirement, you could also use this way:
根据要求,您还可以使用这种方式:
var v = $("#yourid").val();
$("#yourid option[value="+v+"]").text()
回答by Ansel Santosa
If you're using a <select>
, .val()
gets the 'value' of the selected <option>
. If it doesn't have a value
, it may fallback to the id
. Put the value you want it to return in the value
attribute of each <option>
如果您使用的是<select>
,则.val()
获取所选<option>
. 如果它没有value
,它可能会回退到id
。将您希望它返回的值放入value
每个的属性中<option>
Edit:See comments for clarification on what value
actually is (not necessarily equal to the value
attribute).
编辑:请参阅评论以澄清value
实际是什么(不一定等于value
属性)。
回答by N00b Pr0grammer
This has worked for me!
这对我有用!
$('#selected-option option:selected').val()
Hope this helps someone!
希望这可以帮助某人!
回答by user3205632
var sal = $('.selectSal option:selected').eq(0).val();
selectSal
is a class.
selectSal
是一个类。
回答by Fereydoon Barikzehy
Try this:
尝试这个:
var text = $('#YourDropdownId').find('option:selected').text();
回答by user3166722
Another option:
另外一个选项:
$("#<%=dropDownId.ClientID%>").children("option:selected").val();
回答by lhagemann
As has been pointed out ... in a select
box, the .val()
attribute will give you the value of the selected option. If the selected option does not have a value attribute it will default to the display value of the option (which is what the examples on the jQuery documentation of .val
show.
正如已经指出的......在一个select
框中,该.val()
属性将为您提供所选选项的值。如果选定的选项没有 value 属性,它将默认为选项的显示值(这是jQuery 文档中.val
的示例显示的内容。
you want to use .text()
of the selected option:
您要使用.text()
所选选项:
$('#Crd option:selected').text()
$('#Crd option:selected').text()
回答by jeswin
This gives you the current value of the drop down, if the drop down has an ID of "someId".
如果下拉列表的 ID 为“someId”,这将为您提供下拉列表的当前值。
$("#someId").val();
回答by Abhijit Patra
Pass the id and hold into a variable and pass the variable where ever you want.
将 id 和 hold 传递到一个变量中,然后将变量传递到您想要的任何位置。
var temp = $('select[name=ID Name]').val();
var temp = $('select[name=ID Name]').val();