判断checkbox是否被选中大概有下面四种方法
$(".check_class").prop("checked") 返回boolean
$(".check_class").is(":checked")返回boolean
$(".check_class").attr("checked"); 返回boolean
循环的话还可以:
$(".check_class").each(function(){
this.checked 返回 boolean
}
<html>
<head>
<input type="checkbox" class="">
</head>
<body>
<div>
<input type="button" class="check_all" value="全选">
<input type="button" class="check_opposite" value="反选">
</div>
<div>
<input type="checkbox" class="check_class" value="small">小
<input type="checkbox" class="check_class" value="middel">中
<input type="checkbox" class="check_class" value="big">大
</div>
<script type="text/javascript">
$(function(){
$(".check_all").click(function(){
$(".check_class").prop("checked", this.checked);
//$(".check_class").attr("checked","checked");--有的时候不管用,原因详见上一篇(.prop()和 .attr()区别)
});
$(".check_opposite").click(function(){
$(".check_class").each(function(){
$(this).prop("checked", !this.checked);
//$(this).attr("checked",!$(this).attr("checked"));--有的时候不管用,原因详见上一篇(.prop()和 .attr()区别)
});
$(".check_class").prop("checked", this.checked);
});
})
</script>
</body>
</html>