基于复选框检查和取消选中来推送和拼接数组的值

时间:2016-09-06 08:37:19

标签: jquery arrays

下面是我的复选框

"<input type='checkbox' name='documents' value='" + item.ID + "' id='chkSelect_" + counter + "' onclick='getSelectedChk(this.value, this.id, \"" + item.NameOfDocument + "\", \"" + item.ApprovalStatus + "\", \"" + item.SelfAttested + "\",\"" + item.MIME + "\")' >" +

我在ajax成功回调中写了这个,点击下面这个复选框的函数叫做

var selected = [];
var selectedItemsToDelete = [];
var selectedItemsToCopy = [];
var selectedItemsToPreview = [];
var selectedItemsToDownload = [];
var selectedItemsToShare = [];
var selectedItemsToEsign = [];
var selectedCheckBoxes;
var selectedDocType;
var loadedResults;
var called;

function getSelectedChk(val, id, doc, stat,SelfAttested,MIME){
if ($("#"+id).is(":checked")) {
   selected.push(val);
   selectedItemsToDelete.push({"ID":val, "NameOfDocument": doc, "ApprovalStatus": stat});
   selectedItemsToCopy.push({"ID":val});
   selectedItemsToPreview.push({"ID":val, "NameOfDocument": doc});
   selectedItemsToDownload.push({"ID":val});
   selectedItemsToShare.push({"ID":val, "NameOfDocument": doc, "ApprovalStatus": stat});
  selectedItemsToEsign.push({"ID": val, "NameOfDocument": doc, "ApprovalStatus": stat, "SelfAttested": SelfAttested, "MIME":MIME});
}
else if(!$("#"+id).is(':checked')){
    selected.splice( $.inArray(val, selected), 1 );
    selectedItemsToDelete.splice( $.inArray({"ID":val, "NameOfDocument": doc, "ApprovalStatus": stat}, selectedItemsToDelete), 1 );
    selectedItemsToCopy.splice( $.inArray({"ID":val}, selectedItemsToCopy), 1 );
    selectedItemsToPreview.splice( $.inArray({"ID":val, "NameOfDocument": doc}, selectedItemsToPreview), 1 );       
    selectedItemsToDownload.splice( $.inArray({"ID":val}, selectedItemsToDownload), 1 );
    selectedItemsToShare.splice( $.inArray({"ID":val, "NameOfDocument": doc, "ApprovalStatus": stat}, selectedItemsToShare), 1 );
    selectedItemsToEsign.splice( $.inArray({"ID": val, "NameOfDocument": doc, "ApprovalStatus": stat, "SelfAttested": SelfAttested, "MIME":MIME}, selectedItemsToEsign), 1 );
}  
}

在这里,当我取消选中该复选框时,只有在我选中并取消选中相同的复选框时才会正确拼接值。如果我选中所有复选框并从复选框中取消选中一个随机复选框,它会拼接最新的数组推送值,而不是拼接取消选中复选框的值。

我可以在我的功能中写出什么样的条件来实现这一目标。

0 个答案:

没有答案