簡體   English   中英

在javascript中編輯輸入字段的名稱

[英]edit name of input fields in javascript

我在一個表中有很多輸入字段,每個輸入字段就像這樣的數組索引

<input type="text" name="customproperty[0]" />
<input type="hidden" name="customproperty[0]" />
<input type="text" name="customproperty[1]" />
<input type="hidden" name="customproperty[1]" />
.
.

有時客戶端刪除字段並破壞順序;

<input type="hidden" name="customproperty[0]" />
<input type="text" name="customproperty[2]" />..

我希望所有這些對象都是順序的,所以我每次在發布它之前都要調用此方法。

function arrangeInputNames() {
    debugger
    var inptSlctr = $("#container");
    var allinputs = inptSlctr .find("input")
    allinputs.each(function (index, el) {
        var newIndex = Math.floor(index / 2);//since there is 2 element for each index
        el.name = el.name.replace("old name"," new name");//regex or ?
    });
}

我需要這樣的正則表達式或任何智能業務邏輯來更改“僅索引號”,這意味着更改后的exp不應從“ customproperty [2]”更改為“ customproperty [1]”,而應將“ 2”更改為“ 1”,因為屬性名稱始終相同,只有索引可以更改

使用正則表達式,您可以執行以下操作:

el.name = el.name.replace(new RegExp("\[[\d]+\]"), "[" + newIndex + "]")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM