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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:48:00  来源:igfitidea点击:

jQuery selector, contains to equals

javascriptjqueryjquery-selectors

提问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 selectand 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 valueattribute of the options, you need to use the attribute equals selectorto select elements with a specific value for their valueattribute:

如果要根据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.

这个答案的概念很好用,你可以在这里看到它的实际效果