如何使用jQuery获取select选项的标签? 标签
可通过如何使用jQuery获取select选项的标签? 标签,jquery,select,Jquery,Select,可通过$select.val()检索该值 那么标签呢 是否有一种解决方案可以在IE6中使用?您正在寻找的$select.html() 试试这个: <select> <option value="test">label </option> </select> <select id="selecter"> <option value="test" label="GET THIS"> Option (also called la
$select.val()
检索该值
那么标签
呢
是否有一种解决方案可以在IE6中使用?您正在寻找的
$select.html()
试试这个:
<select>
<option value="test">label </option>
</select>
<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>
“option:eq(0)”中的0索引可以替换为您想要检索的任何索引选项。Hi首先为select as提供一个id
$("select#selectbox option:eq(0)").text()
要在下拉列表中获取特定选项的标签,您可以使用以下命令--
或
作为参考,选项标签上还有一个次要的标签
属性:
$('#id_of_dropdown > option[value='value_to_be_Searched']').html();
Html
选项(也称为标签)
试试这个:
<select>
<option value="test">label </option>
</select>
<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>
这将拉出两种样式的
元素的显示文本:
->“foo”
bar
->“bar”
如果元素中既有标签
属性又有文本,那么它将使用标签
属性,这与浏览器的行为相同
对于后代来说,这是在jQuery 3.1.1下测试的,jQuery 3.1.1为此创建了工作Plunk。
$(“#控制台”).append(
“++$(“#测试:选定”).text()
在现代浏览器中,您不需要JQuery来完成此操作。改用
$('select option:selected').prop('label');
或者指定任何DOM元素而不是文档
我发现这很有用
document.querySelectorAll('option:checked')
使用此
关键字访问选择器时
$('select[name=users] option:selected').text()
你的意思是如何得到选中的值,选中的值?哪个是您的案例标签?这个问题应该改写为“如何使用jQuery获取选择选项的文本?”并且所有对标签的引用都应该替换为文本,以避免与标签属性混淆。这只是返回所有选项元素的html。标签文本在某处,但这不是获取它的最有效方法。这并不总是正确的。选项显示的描述也可以由“label”属性指定(除了获取label属性外,还可以使用:jQuery(“#theid option:selected”).attr('label'))
<select id="selecter">
<option value="test" label="GET THIS">
Option (also called label)</option>
</select>
$('select option:selected').prop('label');
document.querySelectorAll('option:checked')
$('select[name=users] option:selected').text()
$(this).find('option:selected').text()