Jquery “select all” checkbox

后端 未结 11 644
执念已碎
执念已碎 2020-12-06 05:45

I am trying to select all checkboxes on a page once the \'select all\' checkbox is clicked and i am using jquery for this as you can see at the below link:

http://js

11条回答
  •  温柔的废话
    2020-12-06 06:30

    A small enhancement to @letiagoalves solution, if your checkboxes have specific processing associated with a click event. Not asked for by the OP, but something I've had to do in the past. Basically, instead of directly setting the checked property of each dependent checkbox, it compares the checked state of each dependent checkbox to the selectall checkbox, and triggers a click event on those that are in the opposite state.

    $('#selectall').click(function () {
        var parentState = $(this).prop('checked');
        $('.selectedId').each(function() {
            if ($(this).prop('checked') !== parentState) {
                $(this).trigger("click");
            }
    });
    });
    

    DEMO: https://jsfiddle.net/jajntuqb/2/

提交回复
热议问题