简体   繁体   English

jQuery选择器,包含等于

[英]jQuery selector, contains to equals

I have the folowing selector 我有下面的选择器

    var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
                                            .find("option:contains('LIKE')");

this checks for an option which contains the word 'like' right? 这会检查包含 “喜欢”这个词的选项吗?

How do i find an option which is exactly with the word 'like'? 我如何找到一个与“喜欢”这个词完全相同的选项?

Somthing like this: 像这样的东西:

     var likeComperssionOption = $('select[id*=ComparisionType]').eq(0)
                                            .find("option:equals('LIKE')");

只需选择从所有选项select ,并通过它们进行过滤text()值:

var likeComperssionOption = $('select[id*=ComparisionType]:first option').filter(function() { return $(this).text() == "LIKE" });

If you want to select based on the value attribute of the options, you need to use the attribute equals selector to select elements with a specific value for their value attribute: 如果要根据选项的value属性进行选择,则需要使用属性equals selector选择具有其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 . 这个答案的概念很好,你可以在这里看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM