<select>
<option value="test">label </option>
</select>
该值可以通过$select.val()
检索。如何使用jQuery获取选择选项的标签?
那么label
呢?
是否有解决方案,将在IE6中工作?
<select>
<option value="test">label </option>
</select>
该值可以通过$select.val()
检索。如何使用jQuery获取选择选项的标签?
那么label
呢?
是否有解决方案,将在IE6中工作?
试试这个:
$('select option:selected').text();
这并不总是正确的。该选项的显示描述也可以通过'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
获取_label_属性,可以使用:jQuery('#theid option:selected')。attr('label') – 2014-04-10 16:07:59
<SELECT id="sel" onmouseover="alert(this.options[1].text);"
<option value=1>my love</option>
<option value=2>for u</option>
</SELECT>
$("select#selectbox option:eq(0)").text()
的0指数中的 “选项:EQ(0)” 即可交换您想要检索的索引选项。
这是有帮助的: http://www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html
嗨先给一个ID,选择作为
<select id=theid>
<option value="test">label </option>
</select>
,那么你可以拨打所选的标签那样:
jQuery('#theid option:selected').text()
这正是我一直在寻找的! – DirtyBirdNJ 2013-08-12 05:10:44
要获得一个特定的选项在下拉列表中的标签哟可以这样 -
$('.class_of_dropdown > option[value='value_to_be_searched']').html();
或
$('#id_of_dropdown > option[value='value_to_be_Searched']').html();
为了参考还存在的选项标签的二次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>
尝试这种情况:
$('select option:selected').prop('label');
这将拉出显示文本两种风格<option>
元素:
<option label="foo"><option>
- >"foo"
<option>bar<option>
- >"bar"
如果它既有一个label
属性和文本在元素内部,它将使用label
属性,这与浏览器的行为相同。
留给后人,这是jQuery的3.1.1
下测试的创工作Plunker这一点。 https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen $('#console').append("<br/>"+$('#test_s :selected').text())
您的意思是如何获得选定的值,选定的值?这是你的情况标签? – ant 2010-02-01 09:47:47
这个问题应该重写为“如何获得jQuery的选择选项**文本**”?并且所有对标签的引用都应该用文本替换以避免与标签属性混淆。 – 2015-11-13 00:35:55