简体   繁体   中英

Jquery Check all checkbox

Why does my check all button work once and doesn't work at 3rd click. the check all button only works at firts click. I check the dom and its updating but the view does. What is the cause of the problem?

FIDDLE

jQuery('.sp_country').click(function () {
    var checkbox = $(this).find(":checkbox"),
        checked = checkbox.is(":checked");
    checkbox.prop("checked", !checked);

});

jQuery('.sp_country input:checkbox').on('click', function (e) {
    e.stopImmediatePropagation();
    var checked = (e.currentTarget.checked) ? false : true;
    e.currentTarget.checked = (checked) ? false : checked.toString();
});

jQuery('#bt_checkbox').click(function (e) {
    e.stopImmediatePropagation();
    if (jQuery(this).val() === 'Uncheck All') {
        jQuery('#country input:checkbox').removeAttr('checked');
        jQuery(this).val('Check All');
    } else {
        jQuery('#country input:checkbox').attr('checked', 'checked');
        jQuery(this).val('Uncheck All');
    }
});

fiddle Demo

Change

jQuery('#country input:checkbox').attr('checked', 'checked');

to

jQuery('#country input:checkbox').prop('checked', true);


Use .prop()

Read .prop() vs .attr()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM