首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >编辑javascript中输入字段的名称

编辑javascript中输入字段的名称
EN

Stack Overflow用户
提问于 2017-09-21 11:45:52
回答 1查看 45关注 0票数 0

我在一个表中有许多输入字段,每个输入字段都像这样的数组的索引

代码语言: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]" />
.
.

有时客户端删除字段和顺序顺序中断;

代码语言:javascript
复制
<input type="hidden" name="customproperty[0]" />
<input type="text" name="customproperty[2]" />..

我希望它们都是连续的,所以每次在发布之前我都会调用这个方法;

代码语言:javascript
复制
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”,因为属性名称总是相同的,只有索引才能更改。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-21 11:52:15

使用regex,您可以:

代码语言:javascript
复制
el.name = el.name.replace(new RegExp("\[[\d]+\]"), "[" + newIndex + "]")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46343117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档