如果第一选择了,第二个没选,点击的话会进入else里边。
我想的是如果全没选或者某个没选,点击让他全部选择。
$("input[type=checkbox]")得到的是2个checkbox,
!$("input[type=checkbox]").attr("checked")验证的为什么只是第一个选中没选中呢?
<a href="javascript:;" class="read" id="all-read">全部标为已读</a>
<input type="checkbox" name="check_new">
<input type="checkbox" name="check_new">
$("#all-check").on("click",function(){
if(!$("input[type=checkbox]").attr("checked")){
$("input[type=checkbox]").attr("checked",true);
}
else{
$("input[type=checkbox]").each(function(){
if ($(this).attr("checked"))
{
$(this).attr("checked",false);
}else{
$(this).attr("checked",true);
}
});
}
})
jQuery
官方也说了对于属性的值是布尔值类型的话,建议使用prop()
方法!所以把
attr
都改成prop
可以解决你的问题。