jQuery 如何获取下拉列表选定值的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10411568/
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
How to get the text of the selected value of a dropdown list?
提问by guitarlass
Possible Duplicate:
jQuery get specific option tag text
How to get the text of the selected option of a select using jquery?
I have a dropdown list and I want to know the text of the selected item. For example:
我有一个下拉列表,我想知道所选项目的文本。例如:
<select>
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
</select>
If I know the selected value, how can I get it's text value? For instance, if the value is 1
how can I get Volvo
?
如果我知道选定的值,我怎样才能获得它的文本值?例如,如果值是1
我怎样才能得到Volvo
?
Help much appreciated.
非常感谢帮助。
回答by Rory McCrossan
You can use option:selected
to get the chosen option of the select
element, then the text()
method:
您可以使用option:selected
来获取select
元素的选择选项,然后是text()
方法:
$("select option:selected").text();
Here's an example:
下面是一个例子:
console.log($("select option:selected").text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select>
<option value="1">Volvo</option>
<option value="2" selected="selected">Saab</option>
<option value="3">Mercedes</option>
</select>
回答by neohope
$("#select_id").find("option:selected").text();
It is helpful if your control is on Server side. In .NETit looks like:
如果您的控件在服务器端,这会很有帮助。在.NET 中,它看起来像:
$('#<%= dropdownID.ClientID %>').find("option:selected").text();
回答by Neysor
The easiest way is through css3 $("select option:selected")
and then use the .text()
or .html()
function. depending on what you want to have.
最简单的方法是通过 css3$("select option:selected")
然后使用.text()
or.html()
函数。取决于你想要什么。
回答by Dhrumil Bhankhar
Hi if you are having dropdownlist like this
嗨,如果你有这样的下拉列表
<select id="testID">
<option value="1">Value1</option>
<option value="2">Value2</option>
<option value="3">Value3</option>
<option value="4">Value4</option>
<option value="5">Value5</option>
<option value="6">Value6</option>
</select>
<input type="button" value="Get dropdown selected Value" onclick="getHTML();">
after giving id to dropdownlist you just need to add jquery code like this
在给下拉列表提供 id 后,你只需要添加这样的 jquery 代码
function getHTML()
{
var display=$('#testID option:selected').html();
alert(display);
}