jQuery filter by an attribute value

后端 未结 3 1179
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-12 11:27
Driver License State Email <
相关标签:
3条回答
  • 2021-02-12 12:08

    Not claiming this is any more elegant, but using filter() on a collection allows much more flexibility on what you can match on, and is a little less error prone than string concatenation.

    var matching = $('.selectedColumns a').filter(function(){
                       return $(this).attr('attributeid') == 41
                    });
        matching.prop('selected', true);
    
    0 讨论(0)
  • 2021-02-12 12:17

    There's no need for the : or double = in your attribute-equals selector, it should just be:

    $('.selectedColumns a[attributeid=' + $(this).val() + ']');
    

    Also if you're using invalid attributes, consider using data- attributes which are valid in HTML5, for example data-id instead of attributeid.

    0 讨论(0)
  • 2021-02-12 12:18

    use a single = instead of 2. Also, the : shoudn't be there afaik

    var link = $('.selectedColumns a[attributeid=' + $(this).val() + ']');
    
    0 讨论(0)
提交回复
热议问题