jQuery 如何使用jQuery获取选择选项的标签?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2175737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:56:08  来源:igfitidea点击:

How to get label of select option with jQuery?

jqueryselect

提问by user198729

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

The value can be retrieved by $select.val().

该值可以通过 检索$select.val()

What about the label?

怎么样的label

Is there a solution that will work in IE6?

是否有适用于 IE6 的解决方案?

回答by Sergey Kuznetsov

Try this:

尝试这个:

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

回答by open-ecommerce.org

Hi first give an id to the select as

嗨,首先给选择一个id

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

then you can call the selected label like that:

然后您可以像这样调用所选标签:

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

回答by kingPuppy

For reference there is also a secondary labelattribute on the option tag:

作为参考label,选项标签上还有一个辅助属性:

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

Html

html

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

回答by Snigdha Batra

To get the label of a specific option in a dropdown yo can ty this --

要在下拉列表中获取特定选项的标签,您可以输入 -

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

or

或者

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

回答by bmatovu

I found this helpful

我发现这很有帮助

$('select[name=users] option:selected').text()

When accessing the selector using the thiskeyword.

使用this关键字访问选择器时。

$(this).find('option:selected').text()

回答by eugene

$("select#selectbox option:eq(0)").text()

The 0 index in the "option:eq(0)" can be exchanged for whichever indexed option you'd like to retrieve.

“option:eq(0)”中的 0 索引可以交换为您想要检索的任何索引选项。

This is helpful: http://www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html

这很有帮助:http: //www.myphpetc.com/2009/03/jquery-select-element-cheat-sheet.html

回答by amphetamachine

Try this:

尝试这个:

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

This will pull out the displayed text for both styles of <option>elements:

这将拉出两种<option>元素样式的显示文本:

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

If it has both a labelattribute and text inside the element, it'll use the labelattribute, which is the same behavior as the browser.

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

For posterity, this was tested under jQuery 3.1.1

对于后代,这是在 jQuery 3.1.1 下测试的

回答by itwasnoteasy

Created working Plunker for this. https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen$('#console').append("<br/>"+$('#test_s :selected').text())

为此创建了工作Plunker。 https://plnkr.co/edit/vR9aGoCwoOUL9tevIEen$('#console').append("<br/>"+$('#test_s :selected').text())

回答by John Henckel

In modern browsers you do not need JQuery for this. Instead use

在现代浏览器中,您不需要为此使用 JQuery。而是使用

document.querySelectorAll('option:checked')

Or specify any DOM element instead of document

或者指定任何 DOM 元素而不是 document

回答by meo

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