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都會被選中
$('#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);
});
}
});
3.複選變單選的js
$('.xuanze').click(function(){
$('.xuanze').removeAttr('checked');
$(this).prop('checked',true);
});