【问题标题】:How to remove existing values from an Array?如何从数组中删除现有值?
【发布时间】:2017-09-27 09:00:29
【问题描述】:

在此脚本中,我选择选中的 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());
    });
};

【问题讨论】:

  • 您没有连接各个值。相反,您只需在每次迭代中覆盖hidCID.value。使用一些分隔符并连接。另请注意,split("") 会将字符串拆分为单个字符。你需要一个分隔符。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2019-09-05
    • 2023-03-13
    • 1970-01-01
    相关资源
    最近更新 更多