18

I have a table with a checkbox at the start of each row. Each Checkbox has the ID of #tablecheckbox. The table header row has a check icon which should check all boxed in the table. How can I do this with jQuery?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
JT Nolan
  • 1,200
  • 1
  • 9
  • 17

2 Answers2

6

Here head_checkbox is id of top header and person class is all row checkbox

 $('#head_checkbox').on('change', function () {
            if ($(this).is(':checked')) {
                $('.person').attr('checked', true);
            } else {
                $('.person').attr('checked', false);
            }
        });

        $('.person').click(function () {
            var total_length = $('.person').length;
            var total_checked_length = $('.person:checked').length;

            if (total_length == total_checked_length) {
                $('#head_checkbox').attr('checked', true);
            } else {
                $('#head_checkbox').attr('checked', false);
            }
        });
Jay Hardia
  • 746
  • 6
  • 16
2
       $('#head_checkbox').click(function () {
            if ($(this).is(':checked')) {
                $('.person').attr('checked', true);
            } else {
                $('.person').attr('checked', false);
            }
        });


        $('.person').click(function () {
            if ($('.person').length == $('.person:checked').length) {
                $('#head_checkbox').attr('checked', true);
            } else {
                $('#head_checkbox').attr('checked', false);
            }
        });
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193