开发者

jQuery Checkbox Selector With Multi Attributes

I've got a jQuery selector thats not working desipte my best efforts to fix it. Any advic开发者_如何学运维e?

            $(':checkbox:(not(:checked))[name=this.name][value="Done"]').attr({
                checked: 'checked',
                disabled: 'disabled'
            });

It should check and disable a checkbox if it's not already checked, has a name attribute = this.name and a value of Done.

I've been fiddling around with it http://jsfiddle.net/PottyMonster/s8RMJ/ I dunno if that'll work though.


This should do it:

$('input[name=' + this.name + '][value=Done][type=checkbox]:not(:checked)').attr({
    checked: 'checked',
    disabled: 'disabled'
});

Updated example: http://jsfiddle.net/s8RMJ/22/

You need to concatenate the value of this.name into the selector.

I also added input to the selector to make it faster.

Also, you had an extra () around the :not() so it looked like :(not()) which isn't valid.


EDIT:

Just wanted to note that I had replaced the :checkbox selector with [type=checkbox] in order to make your selector valid for querySelectorAll.

Now it will get a big performance boost in browsers that support qsa.


change [name=this.name] to [name="'+this.name+'"]
and :checkbox to [type="checkbox"]
http://jsfiddle.net/s8RMJ/24/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜