我在一个表中有许多输入字段,每个输入字段都像这样的数组的索引
<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 ?
});
}我需要这样的正则表达式或任何智能的业务逻辑来更改“只有索引号”,我的意思是上面更改的实例不应该是"customproperty2“改为"customproperty1”,而是"2“到"1”,因为属性名称总是相同的,只有索引才能更改。
发布于 2017-09-21 11:52:15
使用regex,您可以:
el.name = el.name.replace(new RegExp("\[[\d]+\]"), "[" + newIndex + "]")https://stackoverflow.com/questions/46343117
复制相似问题