如何从数组中删除现有值?

时间:2017-04-29 09:10:51

标签: javascript arrays model-view-controller hidden-field

在这个脚本中,我选择了已检查的id并将字符串存储在隐藏字段中 为了删除存在的id,我将字符串存储在数组中 我想从数组中删除存在的id并将数组值存储到 隐藏的领域。

脚本:

AddRemoveCustomer = function(){
    $(".checkBoxClass").click(function (e) {
        //var CustomerIDArray = '';
        var hidCID = document.getElementById("hfCustomerID");
        var CustArray = [];
        if(hidCID!=null || hidCID!=undefined)
        {
            var CustID = hidCID.value;
            CustArray = CustID.split("");

            alert('CustID is :' + CustID );
            alert('CustArray is :' + CustArray );
            if (CustID == null || CustID=="") {
                $(".checkBoxClass:checked").each(function () {
                    alert("Null");
                    //CustomerIDArray = $(this).val();
                    //alert(CustomerIDArray);
                    alert('value : '+$(this).val());
                    hidCID.value = $(this).val();
                });
            } else {
                alert("Not Null");
                //CustomerIDArray = CustID;
                $(".checkBoxClass:checked").each(function () {
                    alert($(this).val());
                    //CustomerIDArray = CustomerIDArray + "," + $(this).val();
                    //alert(CustomerIDArray);
                    CustID = CustID +","+ $(this).val();
                    alert(CustID);
                    alert('value : '+$(this).val());
                    hidCID.value = CustID;
                });
            }
            //CustID = CustomerIDArray.values;
        }
        //$('#hfCustomerID').val(allSelectedValues);
        //alert($('#hfCustomerID').val());
    });
};

1 个答案:

答案 0 :(得分:0)

要删除数组项,请使用Array.splice(startIndex, deleteCount)

let elIndex = custArray.indexOf(deleteId);
custArray.splice(elIndex, 1);