在 jQuery 中获取 SELECT 的值和文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12614308/
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 SELECT's value and text in jQuery
提问by CL22
Possible Duplicate:
Getting the value of the selected option tag in a select box
可能的重复:
在选择框中获取所选选项标签的值
For a SELECT box, how do I get the value and text of the selected item in jQuery?
对于 SELECT 框,如何在 jQuery 中获取所选项目的值和文本?
For example,
例如,
<option value="value">text</option>
回答by Vishal Suthar
<select id="ddlViewBy">
<option value="value">text</option>
</select>
JQuery
查询
var txt = $("#ddlViewBy option:selected").text();
var val = $("#ddlViewBy option:selected").val();
回答by Zahid Riaz
$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value
回答by Sushanth --
$('select').val() // Get's the value
$('select option:selected').val() ; // Get's the value
$('select').find('option:selected').val() ; // Get's the value
$('select option:selected').text() // Gets you the text of the selected option
回答by Fredy
You can do like this, to get the currently selected value:
您可以这样做,以获取当前选定的值:
$('#myDropdownID').val();
& to get the currently selected text:
& 获取当前选中的文本:
$('#myDropdownID:selected').text();
回答by swapnesh
on the basis of your only jQuery
tag :)
基于你唯一的jQuery
标签:)
HTML
HTML
<select id="my-select">
<option value="1">This is text 1</option>
<option value="2">This is text 2</option>
<option value="3">This is text 3</option>
</select>
For text--
对于文字——
$(document).ready(function() {
$("#my-select").change(function() {
alert($('#my-select option:selected').html());
});
});
For value--
对于价值——
$(document).ready(function() {
$("#my-select").change(function() {
alert($(this).val());
});
});