87,993
社区成员
发帖
与我相关
我的任务
分享<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>jQuery实现CheckBox添加、全选、全不选</title>
<meta charset="utf-8">
<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#add").click(function(){
$("<input type='checkbox' name='subBox'>").appendTo("#addto");
$("#checkAll").click(function() {
$(":checkbox").attr("checked",this.checked);
});
var $subBox = $("input[name='subBox']");
$subBox.click(function(){
$("#checkAll").attr("checked",$subBox.length ==$("input[name='subBox']:checked").length ? true : false);
});
});
});
</script>
</head>
<body>
<div id="addto">
<input id="checkAll" type="checkbox" />全选
</div>
<br/><input type="button" name="add" id="add" value="添加">
</body>
</html>
把所有的代码用“$("#add").click(function(){ });”包起来即可。