开发者

Question regarding checking the state of a checkbox class

I'll try and make this question simple. Can I assign a class to a series of different checkboxes, and use Jquery to do something when any one of those checkboxes is checked? Searching around on the internet I have found documentation on grabbing the name: $('input[name=foo]').is(':checked') but when I swap out the name attribut开发者_高级运维e for the class attr, it won't work! How can I set an event to occur if any of the checkboxes with this certain class is checked? Please help me out! Thanks


Use hasClass to test whether or not a particular element has been assigned a certain class e.g.:

$(document).ready(function() {
    $("input[name=something]:checkbox").click(function() {
        if($(this).is(":checked") && $(this).hasClass("foo")) {
           // the checkbox has been checked and has a class of foo
        }
    });
});

Try a demo here.


You can get the status of all the checkbox at a single go by putting same name to all the checkboxes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜