Javascript 重置名称属性数组索引,动态添加或删除行

Javascript 重置名称属性数组索引,动态添加或删除行,javascript,html,Javascript,Html,我必须通过ajax发布数据,并且需要重置名称属性数组索引 例如: 第一排: <select data-placeholder="Colours" class="colours chosen-select" multiple tabindex="4" name="colours[0][]"> <option value="">Red</option> <option value="">Orange</option> </selec

我必须通过ajax发布数据,并且需要重置名称属性数组索引

例如:

第一排:

<select data-placeholder="Colours" class="colours chosen-select" 
multiple tabindex="4" name="colours[0][]">
<option value="">Red</option>
<option value="">Orange</option>
</select>
我已经添加了var count以增加索引,但在删除时无法重置值


请帮忙

您可以尝试循环表中的所有select,并在每次删除项目时更改每个select的属性“name”

$('#rowCount tr td select').each(function(idx, item){
 $(item).attr("name", "colours[" + idx + "][]")
});
更新

像suggest@ChayimFriedman,这是之前建议的酷版本

$('#rowCount select').attr('name', function(idx) { return "colours[" + idx + "][]"; });

您是希望在执行ajax调用时更改所有列表的名称,还是只添加一个名称与前一个列表相同(数字除外)的列表?将此名称添加到select元素的目的是什么?@ChayimFriedman。我不确定你的问题?@TheGr8_Nik需要在已经发布的数组中存储一个数组。当你添加一个新列表时,是否希望所有现有列表都用新名称更新?它是如何工作的。当我添加三行,然后删除中间的一行。它将第三个值更改为:name=“colors[7][]”更漂亮-
$('#rowCount select').attr('name',function(idx,item){return“colors[”+idx+“][”;})。谢谢!它似乎适用于:$('.colors').attr('name',function(idx,item){return“colors[“+idx+”][]”;})@mpdev7请参见,您可以传递给
attr()
一个函数,该函数将对每个元素求值。如果它不会返回或将返回
未定义的
,则先前的值不会更改。一个小注释:您可以删除参数
,因为您不使用它。
$(document.body).delegate(".delRowBtn", "click", function () {

  $(this).closest("tr").remove();
 });
$('#rowCount tr td select').each(function(idx, item){
 $(item).attr("name", "colours[" + idx + "][]")
});
$('#rowCount select').attr('name', function(idx) { return "colours[" + idx + "][]"; });