jQuery selector comparing element's attributes (and/or properties) between them

前端 未结 2 1276
生来不讨喜
生来不讨喜 2021-01-28 19:09

The thing starts from my previous question: Using jQuery is it possible to compare two attributes in a selector? I mean something like:

$(\'element[atribute1!=at         


        
相关标签:
2条回答
  • 2021-01-28 19:41

    No. If you look through jQuery's documentation, you'll see there's no such selector.

    Also defaultValue is a property, not an attribute.

    You can do this:

    var changed = $('input').filter(function() {
        return this.value !== this.defaultValue;
    });
    
    0 讨论(0)
  • 2021-01-28 19:54

    You can use the jQuery filter() function, like this:

    var different = $('input').filter(function()
    {
        return $(this).attr('data-default-value') != $(this).val();   
    });
    

    Then, you can test different.length > 0.

    Here's a full working example: http://jsfiddle.net/us47d/

    0 讨论(0)
提交回复
热议问题