<div class="box-content" id="list"> <input id=‘check-All‘ type=‘checkbox‘ onclick="checkAll()"> <input type=‘checkbox‘ name="keys" id="keys"> <div>
function checkAll(){ var sp=$("#check-All").is(‘:checked‘);//判断是否选中 var repayIds=$("#repayIds").val();//获取变量
var box = document.getElementsByName("keys");//获取所有checkbox
if(sp==false){ $("#list :checkBox,check-All").parent(‘span‘).removeClass("checked");
//id为"list"内的div里面的checkBox和id="check-All"的checkBox,取消选中效果
$("#list :checkBox,check-All").attr("checked",false);
//id为"list"内的div里面的checkBox和id="check-All"的checkBox,取消选择 for (var i=0;i<box.length;i++ ){ repayIds=repayIds.replace(box[i].value+"," ,"");
//去掉选择的值 } }else{ $("#list :checkBox,check-All").parent(‘span‘).addClass("checked");
//id为"list"内的div里面的checkBox和id="check-All"的checkBox,变为选中效果
$("#list :checkBox,check-All").attr("checked",true);
//id为"list"内的div里面的checkBox和id="check-All"的checkBox,变为选中
for (var i=0;i<box.length;i++ ){ if(!repayIds.contains(box[i].value)){
//拼接选中的值,","隔开 repayIds+=box[i].value+"," } } } $("#repayIds").val(repayIds);
//存入变量中 }
原文:http://www.cnblogs.com/wangzhuxing/p/5221162.html