18

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?

Help much appreciated.

4

4 回答 4

34

您可以使用option:selected获取select元素的选择选项,然后text()方法:

$("select option:selected").text();

这是一个例子:

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>

于 2012-05-02T10:04:16.473 回答
15
$("#select_id").find("option:selected").text();

如果您的控件位于服务器端,这将很有帮助。在.NET中,它看起来像:

$('#<%= dropdownID.ClientID %>').find("option:selected").text();
于 2012-05-02T10:06:07.317 回答
1

The easiest way is through css3 $("select option:selected") and then use the .text() or .html() function. depending on what you want to have.

于 2012-05-02T10:06:48.970 回答
1

嗨,如果你有这样的下拉列表

<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();">

在将 id 提供给下拉列表后,您只需要添加这样的 jquery 代码

function getHTML()
{
      var display=$('#testID option:selected').html();
      alert(display);
}
于 2012-05-02T10:18:39.647 回答