select all checkbox only works twice

前端 未结 5 992
误落风尘
误落风尘 2021-01-14 02:28

I got this code from stackoverflow which looks like a pretty good \"select all\" checkbox solution, any ideas why it fails after 2nd click?

http://jsfiddle.net/R9zj

5条回答
  •  情书的邮戳
    2021-01-14 03:18

    You code does not work on jQuery 1.9 and is because of framework of jQuery version you have, choose 1.8.3 and it would work.

    Live Demo

    $(document).ready(function () {
        $(document).on("click", ".selectall2", function () {
            $(this).closest('tr').find('input[type=checkbox]').attr('checked', this.checked);
        });
    });
    

    You should use prop instead of attr for jQuery 1.6 and above

    Live Demo

    $(document).ready(function () {
        $(document).on("click", ".selectall2", function () {
            $(this).closest('tr').find('input[type=checkbox]').prop('checked', this.checked);
        });
    });
    

    As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. To retrieve and change DOM properties such as the checked, selected, or disabled state of form elements, use the .prop() method, jQuery doc

提交回复
热议问题