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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:17:40  来源:igfitidea点击:

How to get the text of the selected value of a dropdown list?

jqueryselect

提问by guitarlass

Possible Duplicate:
jQuery get specific option tag text
How to get the text of the selected option of a select using jquery?

可能的重复:
jQuery 获取特定选项标签文本
如何使用 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 1how can I get Volvo?

如果我知道选定的值,我怎样才能获得它的文本值?例如,如果值是1我怎样才能得到Volvo

Help much appreciated.

非常感谢帮助。

回答by Rory McCrossan

You can use option:selectedto get the chosen option of the selectelement, 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);
}