判断复选框是否点击和点击了哪一个

判断复选框哪一个被点击
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
var a=$('[type=checkbox]').length;
alert(a);
for(var i=0;i<a;i++){
 if($('[type=checkbox]').eq(i).is(':checked')) {
         alert(i+1);  
    }
}
});
});
</script>
</head>
<body>
<input type="checkbox" class="a">123
<input type="checkbox" class="b">123
<button>获取被点击的按钮</button>
</body>
</html>
 
只要复选框被点击,不管哪一个都出发事件。
<!DOCTYPE html>
<html>
<head>
<script src="/jquery/jquery-1.11.1.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
 if($('[type=checkbox]').is(':checked')) {
         alert(true);  
}
});
});
</script>
</head>
<body>
<input type="checkbox" class="a">123
<input type="checkbox" class="b">123
<button>获取被点击的按钮</button>
</body>
</html>
posted @ 2016-08-26 00:03  有一个小白  阅读(2853)  评论(0编辑  收藏  举报