jquery select all checkboxes

前端 未结 19 2747
情书的邮戳
情书的邮戳 2020-12-13 12:36

I have a series of checkboxes that are loaded 100 at a time via ajax.

I need this jquery to allow me to have a button when pushed check all on screen. If more are lo

19条回答
  •  生来不讨喜
    2020-12-13 13:31

    The currently accepted answer won't work for jQuery 1.9+. The event handling aspect of the (rather heavily) overloaded .toggle() function was removed in that version, which means that attempting to call .toggle(function, function) will instead just toggle the display state of your element.

    I'd suggest doing something like this instead:

    $(function() {
        var selectAll = $('#selectall');
        selectAll.on('click', function(e) {
            var checked = !(selectAll.data('checked') || false);
            $('#friendslist .tf').prop('checked', checked);
            selectAll.data('checked', checked);
        });
    });
    

    That uses a regular click event handler, plus a data attribute to track the "toggled" status and invert it with each click.

提交回复
热议问题