今天要做一个有点类似邮箱的批量删除多封邮件的功能,于是乎怎样处理就成了关键。
说简单点也就一表格,第一行第一列是一个全选的复选框,其他行第一列是一个选本行的复选框,全选的复选框class为check-all , id为check0,其他复选框的id为check+该条记录的id ,要定义一个全局变量checkids。
// Check all checkboxes when the one in a table head is checked:
$('.check-all').click( //多选的复选框点击后
function(){
$(this).parent().parent().parent().parent().find("input[type='checkbox']").attr('checked', $(this).is(':checked'));
checkids="";//先清空全局变量
if($(this).is(':checked')){
//check-all is checked 判断多选的复选框是否选中 如果选中 找到所有的复选框 并把其id放入全局变量checkids中
var a=$(this).parent().parent().parent().parent().find("input[type='checkbox']");
for(i=1;i<a.size();i++){
checkids+=a[i].id+",";
}
}else{
// 如果没选中则什么也不用做了 do nothing
}
}
);
$("input[type='checkbox']").click(
function(){
if($(this).is(':checked')){//if checked add the id ,判断普通多选框是否被选中 选中 全局变量加其id
checkids+=$(this).attr("id")+",";
}else{// if not checked remove the id 判断普通多选框是否被选中 选中 全局变量加其id 没选中 删其id
var a =$(this).attr("id")+",";
checkids= checkids.replace(a,"");
}
// alert(checkids);
}
);
/**
* 批量删除
*/
function deleteByIds(){
// alert(checkids);
if(checkids==""){
alert("请选择要删除的项!");
}else{
checkids=checkids.replace("check0,","");//把check0去掉
checkids=checkids.substring(0,checkids.length-1);//截取最后的逗号
while(checkids.search("check")>-1){//去掉所有的check字符串
checkids=checkids.replace("check","");
}
}
// alert(checkids);
jQuery.ajax({
type:"POST",
contenttype:"application/json",
async:false,
cache:false,
url:"/ET/app/deleteUserByIds/"+checkids,
datatype:"json",
success:function (data){
if(data==1){
alert("删除记录成功!");
window.location.reload();
}else{
alert("删除记录失败!");
}
},
error:function(data){
alert("删除记录失败!!");
}
});
}
界面如图所示: