tbody是根据list动态生成的表格,绑定的value为list的id字段,现在只是把结构显示出来,具体循环没有贴出来。
<table>
<thead>
<tr>
<th><input type="checkbox" id="selectAll"></th>
</tr>
</thead>
<tbody id="tbody1">
<tr><td><input type="checkbox" class="selectSingle" value="' + list[i].id + '"></td></tr>
</tbody>
</table>
1、多选框的全选反选
$('#selectAll').on('click',function(){
$('.selectSingle').prop('checked',this.checked);
});
$('#tbody1').on('click','.selectSingle',function(){
if($('.selectSingle').length==$('.selectSingle:checked').length){
$('#selectAll').prop('checked',true);
}else{
$('#selectAll').prop('checked',false);
}
})
2、 获取多选框的选中值,并以,号隔开拼接成字符串传给后台。
var checedAll = '';//定义集合
var checkedL = $('#tbody1').find('.selectSingle');//获取所有的多选框节点
for(var i=0;i<checkedL.length;i++){//循环,如果选中就放在集合里
if(checkedL[i].checked){
checedAll+=checkedL[i].value+','
}
}
if(checedAll==''){
layer.msg('请先选择任务');
return;
}else{
checedAll=checedAll.substr(0,checedAll.length-1);
}