使用 jQuery 获取下拉列表项的选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2780566/
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 selected value of a dropdown's item using jQuery
提问by shyam
How can I get the selected value of a dropdown box using jQuery?
I tried using
如何使用 jQuery 获取下拉框的选定值?
我尝试使用
var value = $('#dropDownId').val();
and
和
var value = $('select#dropDownId option:selected').val();
but both return an empty string.
但都返回一个空字符串。
回答by Peter McG
For single select dom elements, to get the currently selected value:
对于单个选择 dom 元素,获取当前选择的值:
$('#dropDownId').val();
To get the currently selected text:
获取当前选中的文本:
$('#dropDownId :selected').text();
回答by Nick Reeve
var value = $('#dropDownId:selected').text()
Should work fine, see this example:
应该可以正常工作,请参阅此示例:
$(document).ready(function(){
$('#button1').click(function(){
alert($('#combo :selected').text());
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="combo">
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>
<input id="button1" type="button" value="Click!" />
回答by Lucky
Try this jQuery,
试试这个 jQuery,
$("#ddlid option:selected").text();
or this javascript,
或者这个javascript,
var selID=document.getElementById("ddlid");
var text=selID.options[selID.selectedIndex].text;
If you need to access the value and not the text then try using val()
method instead of text()
.
如果您需要访问值而不是文本,请尝试使用val()
method 而不是text()
.
Check out the below fiddle links.
查看下面的小提琴链接。
回答by Kvadiyatar
try this
尝试这个
$("#yourDropdown option:selected").text();
回答by DevlshOne
I know this is a terribly old post and I should probably be flogged for this pitiful resurrection, but I thought I would share a couple of VERY helpful little JS snippets that I use throughout every application in my arsenal...
我知道这是一个非常老的帖子,我可能应该因为这个可怜的复活而受到鞭打,但我想我会分享一些非常有用的小 JS 片段,我在我的武器库中的每个应用程序中都使用它们......
If typing out:
如果输入:
$("#selector option:selected").val() // or
$("#selector option:selected").text()
is getting old, try adding these little crumpets to your global *.js
file:
变老了,尝试将这些小松饼添加到您的全局*.js
文件中:
function soval(a) {
return $('option:selected', a).val();
}
function sotext(a) {
return $('option:selected', a).text();
}
and just write soval("#selector");
or sotext("#selector");
instead! Get even fancier by combining the two and returning an object containing both the value
and the text
!
只是写soval("#selector");
或sotext("#selector");
代替!通过将两者结合起来并返回一个包含value
和text
!
function so(a) {
my.value = $('option:selected', a).val();
my.text = $('option:selected', a).text();
return my;
}
It saves me a ton of precious time, especially on form-heavy applications!
它为我节省了大量宝贵的时间,尤其是在形式繁重的应用程序中!
回答by Uthaiah
This will alert the selected value. JQuery Code...
这将提醒选定的值。JQuery 代码...
$(document).ready(function () {
$("#myDropDown").change(function (event) {
alert("You have Selected :: "+$(this).val());
});
});
HTML Code...
HTML代码...
<select id="myDropDown">
<option>Milk</option>
<option>Egg</option>
<option>Bread</option>
<option>Fruits</option>
</select>
回答by Singleton
this will do the trick
这将解决问题
$('#dropDownId').val();
回答by J Slick
Yet another tested example:
另一个经过测试的例子:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$('#bonus').change(function() {
alert($("#bonus option:selected").text());
});
});
</script>
</head>
<body>
<select id="bonus">
<option value="1">-00</option>
<option value="2">-00</option>
<option value="3"><select id='dropDownId'> ...
</option>
<option value="4"></option>
<option value="5">We really appreciate your work</option>
</select>
</body>
</html>
回答by ?ngelo Rigo
Try this, it gets the value:
试试这个,它得到的价值:
$('select#myField').find('option:selected').val();
$('select#myField').find('option:selected').val();
回答by schaechtele
Did you supply your select-element with an id?
您是否为您的选择元素提供了 id?
##代码##Your first statement should work!
你的第一个语句应该有效!