javascript jQuery 选择器,包含等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7344220/
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
jQuery selector, contains to equals
提问by Aharon Muallem
I have the folowing selector
我有以下选择器
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
.find("option:contains('LIKE')");
this checks for an option which containsthe word 'like' right?
这会检查包含“喜欢”一词的选项,对吗?
How do i find an option which is exactlywith the word 'like'?
我如何找到与“喜欢”一词完全一致的选项?
Somthing like this:
像这样的东西:
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
.find("option:equals('LIKE')");
回答by Igor Dymov
Just select all the options from the select
and filter them by text()
value:
只需从 中选择所有选项select
并按text()
值过滤它们:
var likeComperssionOption = $('select[id*=ComparisionType]:first option').filter(function() { return $(this).text() == "LIKE" });
回答by Jon
If you want to select based on the value
attribute of the options, you need to use the attribute equals selectorto select elements with a specific value for their value
attribute:
如果要根据value
选项的属性进行选择,则需要使用属性等于选择器为其value
属性选择具有特定值的元素:
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
.find("option[value='LIKE']")
Otherwise, to select based on the display text of the options, use:
否则,要根据选项的显示文本进行选择,请使用:
var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
.find("option").filter(function() {
return $(this).text() == 'LIKE';
});
Update:You might be having some problems with your initial selector, it looks very strange. You should probably change
更新:您的初始选择器可能有一些问题,看起来很奇怪。你可能应该改变
$('select[id*=ComparisionType]').eq(0)
to something simple like
一些简单的东西
$('#ComparisionType')
The concept of this answer works fine, you can see it in action here.
这个答案的概念很好用,你可以在这里看到它的实际效果。