1.checkbox内容
<div id="formdiv"> <input type="checkbox" name="checkoption">篮球<p> <input type="checkbox" name="checkoption">足球<p> <input type="checkbox" name="checkoption">网球<p> <input type="checkbox" name="checkoption">台球<p> <input type="checkbox" id="selectAll"> <label for="selectAll">全选</label> </div> 2.定义js
<script type="text/javascript"> /* 全选 */ $(document).ready(function(){ $("#selectAll").click(function(){ //获取全选的状态 var state = $("#selectAll").prop("checked"); //同步 $("input[name='checkoption']").prop("checked",state); }); //复选框 $(":checkbox[name='checkoption']").click(function(){ /* 所有的复选框 */ //var all = $(":checkbox[name='checkoption']").length; var all = $("input[name='checkoption']").length; /* 选中的复选框 */ var option = $("input[name='checkoption']:checked").length; if (all == option) { $("#selectAll").prop("checked",true); }else{ $("#selectAll").prop("checked",false); } }); }); </script>
