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);
});
}
});