jQuery 我需要哪个选择器通过文本选择选项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3744289/
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
Which selector do I need to select an option by its text?
提问by Hailwood
I need to check if a <select>
has an option whose text is equal to a specific value.
我需要检查 a<select>
是否有一个选项,其文本等于特定值。
For example, if there's an <option value="123">abc</option>
, I would be looking for "abc".
例如,如果有一个<option value="123">abc</option>
,我会寻找“abc”。
Is there a selector to do this?
有没有选择器来做到这一点?
Im looking for something similar to $('#select option[value="123"]');
but for the text.
我正在寻找类似于$('#select option[value="123"]');
但文本的东西。
回答by Hari Pachuveetil
This could help:
这可以帮助:
$('#test').find('option[text="B"]').val();
This would give you the option with text B
and not the ones which has text that contains B
. Hope this helps
这将为您提供带有文本的选项,B
而不是包含B
. 希望这可以帮助
For recent versions of jQuery the above does not work.As commented by Quandarybelow, this is what works for jQuery 1.9.1:
对于最新版本的 jQuery,上述内容不起作用。正如下面的Quandary所评论的,这适用于 jQuery 1.9.1:
$('#test option').filter(function () { return $(this).html() == "B"; }).val();
回答by SLaks
You can use the :contains()
selectorto select elements that contain specific text.
For example:
您可以使用:contains()
选择器来选择包含特定文本的元素。
例如:
$('#mySelect option:contains(abc)')
To check whether a given <select>
element has such an option, use the .has()
method:
要检查给定<select>
元素是否具有这样的选项,请使用以下.has()
方法:
if (mySelect.has('option:contains(abc)').length)
To find all <select>
s that contain such an option, use the :has()
selector:
要查找<select>
包含此类选项的所有s,请使用:has()
选择器:
$('select:has(option:contains(abc))')
回答by Dr. C. Hilarius
None of the previous suggestions worked for me in jQuery 1.7.2 because I'm trying to set the selected index of the list based on the value from a textbox, and some text values are contained in multiple options. I ended up using the following:
之前的建议在 jQuery 1.7.2 中对我不起作用,因为我试图根据文本框中的值设置列表的选定索引,并且某些文本值包含在多个选项中。我最终使用了以下内容:
$('#mySelect option:contains(' + value + ')').each(function(){
if ($(this).text() == value) {
$(this).attr('selected', 'selected');
return false;
}
return true;
});
回答by Jaydeep Shil
I faced the same issue below is the working code :
我在下面遇到同样的问题是工作代码:
$("#test option").filter(function() {
return $(this).text() =='Ford';
}).prop("selected", true);
回答by Phillip Dennis
This worked for me: $("#test").find("option:contains('abc')");
这对我有用: $("#test").find("option:contains('abc')");
回答by sreejesh
This is the best method for select text in dropdownlist.
这是在下拉列表中选择文本的最佳方法。
$("#dropdownid option:contains(your selected text)").attr('selected', true);
回答by user2561852
I tried a few of these things until I got one to work in both Firefox and IE. This is what I came up with.
我尝试了其中的一些东西,直到我在 Firefox 和 IE 中找到了一个。这就是我想出的。
$("#my-Select").val($("#my-Select" + " option").filter(function() { return this.text == myText }).val());
another way of writing it in a more readable fasion:
另一种以更具可读性的方式编写它的方式:
var valofText = $("#my-Select" + " option").filter(function() {
return this.text == myText
}).val();
$(ElementID).val(valofText);
Pseudocode:
伪代码:
$("#my-Select").val( getValOfText( myText ) );
回答by Dadaso Zanzane
Use following
使用以下
$('#select option:contains(ABC)').val();
回答by Mir Gulam Sarwar
For jquery version 1.10.2 below worked for me
对于下面的 jquery 1.10.2 版对我有用
var selectedText="YourMatchText";
$('#YourDropdownId option').map(function () {
if ($(this).text() == selectedText) return this;
}).attr('selected', 'selected');
});
回答by Carlos Casta?eda
This work for me
这对我有用
$('#mySelect option:contains(' + value + ')').attr('selected', 'selected');