2014-04-02 23 views
0

後,我有以下格式具有表爲TR增量訂單ID刪除行太

<table> 
    <tr> 
     <td> 
     <input type="hidden" name="help[0].id" /> 
     </td> 
     <td> <span class="tr-close">X</span> 
    </tr> 
    <tr> 
     <td> 
     <input type="hidden" name="help[1].id" /> 
     </td> 
     <td> <span class="tr-close">X</span> 
    </tr> 
    <tr> 
     <td> 
     <input type="hidden" name="help[2].id" /> 
     </td> 
     <td> <span class="tr-close">X</span> 
    </tr> 
</table> 

現在我有添加按鈕,這將增加行表的末尾表。在點擊X圖標時,我正在刪除整行。假設如果我刪除名爲help [1] .id的行,現在如果我再次添加新行,它應該有幫助[1] .id ..因此,在每次添加/刪除時,名稱應該具有不同的增量非重複數字像id的0,1,2,3 ....

回答

0

刪除該行後,您可以遍歷每個隱藏字段以更新name屬性。您可以使用.each()進行循環。使用.attr()方法來更新的name屬性

$(".tr-close").click(function(){ 
$(this).parents("tr").remove(); 
$("input[type=hidden]").each(function(i){ 

$(this).attr("name","help["+i+"]"); 
}); 
}); 

Demo

0

你可以像下面這樣做

//In order to have the dynamically created rows to have the 

//delete function also you can use you on() like this  

$('table').on('click', '.tr-close', function(){ 

    $(this).parents('tr').remove(); 

}); 

// the #add refers to the add button you use 

$('#add').click(function(){ 

    var counter = 0; 

    $('table') 

    .append('<tr><td><input type="hidden" name=""/></td><td> <span class="tr-close">X</span></td></tr>'); 

    $('tr').each(function(){ 

    $(this).find('input').prop('name', 'help[' + counter + '].id'); 

    counter++; 

    }); 

}); 
0

我想你可以用「堆」的數據結構來維持的最小數量您刪除的索引。添加新行時從堆中選擇。

RemoveRow -> 
    Insert index to Heap 

AddRow -> 
    if Heap is empty: 
     Index = Rows.length 
    else: 
     Index = top of heap 
     remove top of heap