当我克隆该部分时,如何更改多维数组的第一列,用尽可能少的代码,我知道我可以单独进入并找到每个输入,但这似乎效率低下。例如,如果我有;
<div class="wrapper">
<input type="text" name="height[0][]" />
<input type="text" name="width[0][]" />
<input type="text" name="depth[0][]" />
<input type="text" name="weight[0][]" />
</div>
克隆并编辑后,克隆的输出应如下所示:
<div class="wrapper">
<input type="text" name="height[1][]" />
<input type="text" name="width[1][]" />
<input type="text" name="depth[1][]" />
<input type="text" name="weight[1][]" />
</div>
谢谢
最佳答案
使用map
和replace
(有点繁琐):
var newElems = $(".wrapper").clone(true).find("input").map((index, item) => {
item.name = item.name.replace(/\[(\d)\]/, `[${parseInt($(item).attr("name").match(/\w+\[(\d)\]/)[1]) + 1}]`);
return item;
});
console.log([...newElems]);
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<div class="wrapper">
<input type="text" name="height[0][]" />
<input type="text" name="width[0][]" />
<input type="text" name="depth[0][]" />
<input type="text" name="weight[0][]" />
</div>
关于javascript - 在克隆期间,更改每个输入的多维数组的第一列索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55769965/