A cleaner way to select by multiple possible attribute values?

后端 未结 2 1818
甜味超标
甜味超标 2020-11-29 11:11

Is there a possibility in jQuery to select by multiple possible attribute values without having to use a comma separated list of selectors.

So in stead of:



        
2条回答
  •  隐瞒了意图╮
    2020-11-29 11:49

    You can make a custom jQuery function like this:

    $.fn.filterAttrVals = function (attr, vals) {
        var filter = '[' + attr + '="' + vals.split(',').join('"],[' + attr + '="') + '"]';
        return this.filter(filter);
    };
    

    For your example you could use it in the following way:

    $('#list1 > option').filterAttrVals('value','1,2');
    

提交回复
热议问题