使用 jQuery 在下拉列表中获取选定的值。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3513437/
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 selected value in a dropdown using jQuery.
提问by bragboy
My HTML is below. I need to get the selected value (Scheduled
) using the
<select>
tag. How can this be done using jQuery?
我的 HTML 如下。我需要Scheduled
使用<select>
标签获取选定的值 ( )
。如何使用 jQuery 做到这一点?
<select id="availability" style="display: none;">
<option value="Available">Available</option>
<option selected="selected" value="Scheduled">Scheduled</option>
<option value="Unavailable">Unavailable</option>
</select>
I did a jQuery("#availability")
to get the select tag, but I do not know how to get the selected options' value.
我做了一个jQuery("#availability")
来获取选择标签,但我不知道如何获取所选选项的值。
回答by Sarfraz
Try:
尝试:
jQuery("#availability option:selected").val();
Or to get the text of the option, use text()
:
或者要获取选项的文本,请使用text()
:
jQuery("#availability option:selected").text();
More Info:
更多信息:
回答by Mahdi
The above solutions didn't work for me. Here is what I finally came up with:
上述解决方案对我不起作用。这是我最终想出的:
$( "#ddl" ).find( "option:selected" ).text(); // Text
$( "#ddl" ).find( "option:selected" ).prop("value"); // Value
回答by JasCav
$("#availability option:selected").text();
This will give you the text value of your dropdown list. You can also use .val()instead of .text()depending on what you're looking to get. Follow the link to the jQuery documentation and examples.
这将为您提供下拉列表的文本值。您还可以使用.val()而不是.text() ,具体取决于您要获得的内容。按照链接到 jQuery 文档和示例。
回答by minura
I have gone through all the answers provided above. This is the easiest way which I used to get the selected value from the drop down list
我已经完成了上面提供的所有答案。这是我用来从下拉列表中获取所选值的最简单方法
$('#searchType').val() // for the value
$('#searchType').val() // for the value
回答by Nakendra Pun
$('#availability').find('option:selected').val() // For Value
$('#availability').find('option:selected').text() // For Text
or
$('#availability option:selected').val() // For Value
$('#availability option:selected').text() // For Text
回答by Shahbaz Pirzada
Hello guys i am using this technique to get the values from the selected dropdownlist and it is working like charm.
大家好,我正在使用这种技术从选定的下拉列表中获取值,并且它的工作原理非常迷人。
var methodvalue = $("#method option:selected").val();