<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>checkbox全选-www.jb200.com</title>
</head>
<body>
<input type="checkbox" name="btn" id="btn"/><label for="btn">全选/全不选</label><br/>
<input type="checkbox" name="choose" id="checkbox1"/><label for="checkbox1">选项1</label><br/>
<input type="checkbox" name="choose" id="checkbox2"/><label for="checkbox2">选项2</label><br/>
<input type="checkbox" name="choose" id="checkbox3"/><label for="checkbox3">选项3</label><br/>
<input type="checkbox" name="choose" id="checkbox4"/><label for="checkbox4">选项4</label><br/>
<input type="checkbox" name="choose" id="checkbox5"/><label for="checkbox5">选项5</label><br/>
<input type="checkbox" name="choose" id="checkbox6"/><label for="checkbox6">选项6</label><br/>
<a href="
javascript:;" id="btn2">反选</a>
</body>
</html>
<script type="text/javascript" src="/js/jquery-1.6.1.min.js"></script>
<script type="text/javascript">
$(function(){
var checkboxes = $('input[name=choose]');
var btn = $('#btn');
var btn2 = $('#btn2');
btn.click(function(){
checkboxes.attr('
checked',this.checked);
});
checkboxes.click(function(){
var flag = true;
checkboxes.each(function(){
if(!this.checked) flag = false;
});
btn.attr('checked',flag);
}); // www.jb200.com
btn2.click(function(){
var flag = true;
checkboxes.each(function(){
this.checked = !this.checked;
if(!this.checked) flag = false;
});
btn.attr('checked',flag);
});
});
</script>