为什么 attr() 在第二次调用时不更新 HTML?

Dar*_*ius 1 jquery dom-traversal

I have a script that is supposed to select all descendant input checkbox, when the ancestor checkbox is selected.

见这里:http : //jsfiddle.net/zJPfR/

选中顶层复选框时,将选中下面的所有复选框。如果您取消选中该复选框,则会删除这些检查。但是,如果您尝试第二次执行此操作,则不起作用。

如果我不正确地处理这个问题,请告诉我。

大流士

        $('.poSelect').click(function() {
        var expandBox   = $(this).parents('.pohelpTbl').next('.expandBox');
        var receipts    = expandBox.find('input[type="checkbox"]');

        if ($(this).is(':checked')) {
            var status = true;
        } else {
            var status = false;
        }

        $(receipts).each(function (i) {
            var cb = $(this);
            cb.attr("checked",status);          
        });
    });
Run Code Online (Sandbox Code Playgroud)

Moo*_*man 5

jQuery.attr只使用初始属性。使用.prop而不是.attr更改当前 DOM。
cb.attr("checked",status);=>cb.prop("checked",status);