<select>
<option value="test">label </option>
</select>
可以通过 $select.val()
获取该值。
那么 label
呢?
有没有在 IE6 中也能使用的解决方案?
试试这个:
$('select option:selected').text();
首先给选择器添加一个id:
<select id=theid>
<option value="test">label </option>
</select>
然后你可以像这样调用所选的标签:
jQuery('#theid option:selected').text()
供参考,选项标签还有一个次要的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[name=users] option:selected').text()
使用this
关键字访问选择器。
$(this).find('option:selected').text()
$('.class_of_dropdown > option[value='value_to_be_searched']').html();
或者
$('#id_of_dropdown > option[value='value_to_be_Searched']').html();
试试这个:
$('select option:selected').prop('label');
<option>
元素的显示文本:
<option label="foo"><option>
-> "foo"
<option>bar<option>
-> "bar"
label
属性和元素内部文本,则会使用label
属性,这与浏览器的行为相同。
出于纪念,此内容在jQuery 3.1.1下进行了测试。
$("select#selectbox option:eq(0)").text()
在现代浏览器中,您不需要使用 JQuery 来实现此功能。相反,请使用
document.querySelectorAll('option:checked')
或者指定任何DOM元素而不是document
element.textContent
。 - Flimm<SELECT id="sel" onmouseover="alert(this.options[1].text);"
<option value=1>my love</option>
<option value=2>for u</option>
</SELECT>
已为此创建了可工作的 Plunker。
https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen
$('#console').append("<br/>"+$('#test_s :selected').text())