2010-02-01 88 views
94
<select> 
<option value="test">label </option> 
</select> 

该值可以通过$select.val()检索。如何使用jQuery获取选择选项的标签?

那么label呢?

是否有解决方案,将在IE6中工作?

+0

您的意思是如何获得选定的值,选定的值?这是你的情况标签? – ant 2010-02-01 09:47:47

+0

这个问题应该重写为“如何获得jQuery的选择选项**文本**”?并且所有对标签的引用都应该用文本替换以避免与标签属性混淆。 – 2015-11-13 00:35:55

回答

192

试试这个:

$('select option:selected').text(); 
+3

这并不总是正确的。该选项的显示描述也可以通过'label'属性来指定(除了<= IE7)。请参阅http://www.w3schools.com/tags/att_option_label.asp#gsc.tab=0和http://www.w3.org/TR/html401/interact/forms.html#h-17.6 – 2013-04-02 18:47:46

+1

获取_label_属性,可以使用:jQuery('#theid option:selected')。attr('label') – 2014-04-10 16:07:59

-1

您正在寻找$select.html()

http://api.jquery.com/html/

+1

这只是返回所有选项元素的html。标签文本是/ in/there,但它不是最有效的方式。 – MSpreij 2012-08-31 14:05:59

0
<SELECT id="sel" onmouseover="alert(this.options[1].text);" 
<option value=1>my love</option> 
<option value=2>for u</option> 
</SELECT> 
13

嗨先给一个ID,选择作为

<select id=theid> 
<option value="test">label </option> 
</select> 

,那么你可以拨打所选的标签那样:

jQuery('#theid option:selected').text() 
+0

这正是我一直在寻找的! – DirtyBirdNJ 2013-08-12 05:10:44

4

要获得一个特定的选项在下拉列表中的标签哟可以这样 -

$('.class_of_dropdown > option[value='value_to_be_searched']').html(); 

$('#id_of_dropdown > option[value='value_to_be_Searched']').html(); 
7

为了参考还存在的选项标签的二次label属性:

//returns "GET THIS" when option is selected 
$('#selecter :selected').attr('label'); 

的Html

<select id="selecter"> 
<option value="test" label="GET THIS"> 
Option (also called label)</option> 
</select> 
0

尝试这种情况:

$('select option:selected').prop('label'); 

这将拉出显示文本两种风格<option>元素:

  • <option label="foo"><option> - >"foo"
  • <option>bar<option> - >"bar"

如果它既有一个label属性和文本在元素内部,它将使用label属性,这与浏览器的行为相同。

留给后人,这是jQuery的3.1.1