2013-03-18 47 views
0

我做了一个组件的列表,用户可以自由地添加/删除输入字段行,因此用户可以自由删除/添加一行数据:通过Dojo/jQuery/Javascript重组数组索引的最佳方式是什么?

---- | _ | | ____ | - 删除按钮 -

---- | _ | | ____ | - 删除按钮 -

---- | _ | | ____ | - 删除按钮 -

---- | _ | | ____ | -remove按钮 -

----添加按钮 - ----保存按钮----

然而,这里涉及一个问题:为了要回JSP中的数据被安排在HTML阵列/ sevlet模型,如thie

<input id="row[0]" .../> 
    <input id="row[1]" .../> 
    <input id="row[2]" .../> 
       ...... 
    <input id="row[n]" .../> 

因此,当用户删除行,尤其是在部分的中间,则数组序列肯定搞砸了,因为我只使用基本的jQuery命令来完成这项工作:

 if (confirm(message_confirm_delete)) { 
      j(this).parent().remove(); 
     } 

所以问题是:当用户删除其中一个数组元素时,重新排列所有输入字段数组的最佳方式是什么?

+0

你可以使用http://api.jquery.com/index/作为行的ID吗? – 2013-03-18 21:49:07

回答

1

如果我正确理解你的问题,你说在输入字段中的ID从中间删除一个后是不准确的,对吗?如果是这样的话,像这样的东西会在元素被移除后更新id属性。

<div> 
    <input id="row[0]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div> 
<div> 
    <input id="row[1]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div> 
<div> 
    <input id="row[2]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div> 
<div> 
    <input id="row[3]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div> 
<div> 
    <input id="row[4]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div> 
<div> 
    <input id="row[5]" type="text" class="removableInput"/><button class="remove">Remove</button> 
</div>  

<script> 
    $('button.remove').click(function() { 
     $(this).parent('div').remove(); 

     $('input.removableInput').each(function(index) { 
      $(this).attr('id', 'row[' + index + ']'); 
     });         
    });  
</script> 
+0

感谢您的快速回复。你能否就Dojo重新分配Dom的建议提出任何建议?欣赏。 – Dreamer 2013-03-19 16:55:18

+0

我不是一个道场人,但它看起来像你可以使用attr函数:http://dojotoolkit.org/reference-guide/1.7/dojo/attr.html#dojo-attr。 – clav 2013-03-19 18:19:49

相关问题