最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery if $this attribute selector contains a word - Stack Overflow

programmeradmin3浏览0评论

Is it possible to use the jquery attribute contains selector on $(this)? I cannot find any examples. I am trying to see if input fields contain a certain word.

$("#form input").keyup(function() {
    if ($(this).filter( "[name~='someWord']" )) {
        console.log('yes');
    }
});

The code returns yes for all input even if they don't contain someWord.

Is it possible to use the jquery attribute contains selector on $(this)? I cannot find any examples. I am trying to see if input fields contain a certain word.

$("#form input").keyup(function() {
    if ($(this).filter( "[name~='someWord']" )) {
        console.log('yes');
    }
});

The code returns yes for all input even if they don't contain someWord.

Share Improve this question edited Mar 21, 2016 at 17:25 Naftali 146k41 gold badges247 silver badges304 bronze badges asked Mar 21, 2016 at 16:37 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 5
  • 1 The result of .filter is always a jQuery object wrapping 0 or more elements. It is always a truthy value, regardless of whether the filter returns 0 elements or not. – user229044 Commented Mar 21, 2016 at 16:41
  • 2 What's wrong with $("#form input[name~='someWord']").keyup(handler);??? EDIT: just rereading your question, your posted code seems plelty unrelevant to your expected behaviour, you don't want to check for name attribute but for input value instead (property, not attribute) – A. Wolff Commented Mar 21, 2016 at 16:52
  • @CyberJunkie Is requirement of Question to include call to jQuery() with parameter this : $(this) and jQuery( "[attribute~='value']" ) selector api.jquery./attribute-contains-word-selector within if condition ? "The code returns yes for all input even if they don't contain someWord." Can you create stacksnippets to demonstrate ? – guest271314 Commented Mar 21, 2016 at 17:31
  • I'm not going to play a devil's advocate, but there is a small "war" between Neal and RajaprabhuAravindasamy (Raja for now) going here. I suggest to not look to the vote amount of the answer, but the answer itself. I have used Raja's fiddle (he placed that in his ment somewhere in this post) to show a parision of both answers. updated fiddle example with answer from Neal & Raja. Please use the console (F12 on browser) to check the output and determine for yourself which answer is right one. – KarelG Commented Mar 21, 2016 at 17:37
  • @KarelG in all cases Raja return No and for all mine return yes. interesting. – Naftali Commented Mar 21, 2016 at 18:52
Add a ment  | 

3 Answers 3

Reset to default 5

You have to use .is() at this context,

if($(this).is( "[name~='someWord']" )) {
    console.log('yes');
}

Because .filter() would return a jquery object (element collection), and that would never be false.

You can do this even better with Vanilla JS in the event handler

$("#form input").keyup(function(evt) {
    if (evt.currentTarget.name.indexOf('someWord') > -1) {
        console.log('yes');
    }
});

This checks the name attribute of the actual DOM element and does not add the overhead of the jquery wrapper.

Try

$("#form input").keyup(function() {
    if ($(this).filter("[name~='someWord']").length) {
        console.log('yes');
    }
});

or better:

$("#form input").keyup(function() {
    if ($(this).is("[name~='someWord']")) {
        console.log('yes');
    }
});
发布评论

评论列表(0)

  1. 暂无评论