多维输入:克隆现有项目时更新索引



创建新项时,是否可以更改多维输入的最后一项的索引?

让我解释一下。

我有一个按钮,可以将列表中的项目克隆到我的列表中。每个项目都包含几个属性(日期、类别…(

这是我显示表单时的一个示例:

validation_form[classe][0][matieres][11][resssources][XXXXX][hasBeenNewed]

我所做的是克隆我的所有项目,但目前,索引XXXX没有更新。

当我克隆我的物品时,如何直接更新XXXX?我想我可以数一下项目的数量,然后更改索引,但这会很痛苦,也很耗时。。。

match()中使用正则表达式创建它们的数组,然后更新所需的数组并将((重新连接到

const el = document.querySelector('input');
const prefix = el.name.split('[')[0],
ind = el.name.match(/[(.*?)]/g);
ind[5]= `[AAA]`;
el.name = `${prefix}${ind.join('')}`
console.log(el)
<input name='validation_form[classe][0][matieres][11][ressources][XXXXX][hasBeenRenewed]'/>

经过一点更改,@charlietfl的答案可以工作(必须更改以适应我的情况(

let inputs = new_ressource_clone.find('input');
let new_index = parseInt(count_ressource) + 1;
inputs.each(function() {
let input = $(this)
const prefix = input.attr('name').split('[')[0],
ind = input.attr('name').match(/[(.*?)]/g);
ind[5]= '['+new_index+']';
matiere_zone.attr('data-matiere-count-ressources', new_index);
let new_name = `${prefix}${ind.join('')}`;
input.attr('name', new_name);
})
new_ressource_clone.appendTo(liste_zone);

最新更新