全选 : <input type="checkbox" id="checkAll" /> 1:<input type="checkbox" name="subBox" /> 2:<input type="checkbox" name="subBox" /> 3:<input type="checkbox" name="subBox" /> 4:<input type="checkbox" name="subBox" />
$(function(){ $('#checkAll').click(function(){ $(this).each(function(){ var allFlag = $(this).attr('checked'); $('[name=subBox]:checkbox').attr('checked',allFlag); }); }); $('[name=subBox]:checkbox').click(function(){ var flag = true; var allSelect = true; $('[name=subBox]:checkbox').each(function(index){ flag = $(this).attr('checked'); if(!flag){ allSelect = false; $('#checkAll').attr('checked',flag); } }); if(allSelect){ $('#checkAll').attr('checked',flag); } }); });