2013-03-18 106 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

相關問題