1、HTML代码
<div class="search_cont" id="chk">
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" id="CheckedAll" value="3505,3512,3532,3534,3603,3516">所有类别</label>
</div>
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" value="3505">新闻聚焦</label>
</div>
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" value="3512">政务公开</label>
</div>
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" value="3532,3534">互动交流</label>
</div>
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" value="3603">政策文件</label>
</div>
<div class="checkbox">
<label><input type="checkbox" class="check" name="sids" value="3516">政策解读</label>
</div>
</div>
2、jquery代码实现
//全选/全不选
$("#CheckedAll").click(function () {
if(this.checked){
$("[name=sids]:checkbox").prop('checked',true)
}else {
$("[name=sids]:checkbox").prop('checked',false)
}
});
//复选框的点击事件
$(".check").click(function () {
var flag=true;
$('[name=sids]:checkbox').each(function () {
if(!this.checked){
flag=false
}
});
$("#CheckedAll").prop('checked',flag)
})
function getCheckBoxVal(){
var chk_value =[];
if($("#CheckedAll").prop('checked')){
chk_value.push($("#CheckedAll").val())
}else{
$("#chk").find('input[name="sids"]:checked').each(function(){//遍历,将所有选中的值放到数组中
chk_value.push($(this).val());
});
}
return chk_value.toString();
}