开发者

jQuery selector, contains to equals

开发者 https://www.devze.com 2023-04-03 05:42 出处:网络
I have the folowing selector var likeComperssionOption = $(\'select[id*=ComparisionType]\').eq(0) .find(\"option:contains(\'LIKE\')\");

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')");


Just select all the options from the select and filter them by text() value:

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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号