checkbox属性checked="checked"通过js已设置,但是不勾选

1.通过 attr('checked','checked') 来设置checkbox时,重复点击,虽然checked属性设置正确,但是checkbox没有被勾选 ,如下代码:(代码是全选功能)

$('#ckAll').click(function(){
            if($('#ckAll ').attr('checked') == 'checked'){
                $('#ckAll').removeAttr('checked');
            }else{
                $('#ckAll').attr('checked','checked');
            }
            if($('#ckAll').attr('checked') == 'checked'){
                $('.tab-list .ckbox').each(function(i,n){
                    $(n).attr('checked','checked');
                });
            }else{
                $('.tab-list .ckbox').each(function(i,n){
                    $(n).removeAttr('checked');
                });
            }
        }); 

2. 换成 prop('checked',true) ,当ckAll被选中时,所有列表checkbox都会被选中

//用click或用 change

$('#ckAll').click(function(){
            if($('#ckAll').prop('checked')){
                $('.tab-list .ckbox').each(function(i,n){
                    $(n).prop('checked',true);
                });
            }else{
                $('.tab-list .ckbox').each(function(i,n){
                    $(n).prop('checked',false);
                });
            }
        });

原文地址:https://www.cnblogs.com/hfdp/p/5095038.html