2012-02-11 75 views
0

enter image description here我正在嘗試將新行添加到表中。新行與下一行重複,只有少量更改。新行將刪除嵌套表中的所有行,但會刪除第一行。我創建了一個的jsfiddle鏈接,你可以找到更多的細節和代碼:從重複中刪除行後添加重複行

http://jsfiddle.net/rL5aZ/1/

var rowId = $("#menu").attr("rowId"); 

    var currentRow = $("#" + rowId); 

    var rowToBeAdded = currentRow.next(); 
    rowToBeAdded.clone().insertAfter(currentRow) 
    .find(".TBLCONTENTS:not(:first),.TBLALTCONTENTS:not(:first)").remove(); 

出於某種原因,下面的作品,但上面沒有:

rowToBeAdded.clone().insertAfter(rowToBeAdded).find(".TBLCONTENTS:gt(0),.TBLALTCONTENTS:gt(0)").remove(); 

回答

1

這是你的代碼:

var currentRow = $("#moduleRow"); 
var rowToBeAdded= $("#moduleRow").next().find(".content").not(":first").remove().find(".altcontent").not(":first").remove(); 
$(currentRow).after(rowToBeAdded); 
  • 你需要.clone()你想要複製的元素,否則插入/附加它只會移動它。
  • 你的表結構缺少td(你有<table><tr><table>,應該是<table><tr><td><table>
  • .find(".content").not(":first").remove().find(".altcontent")會先刪除所有.content除了第一節,然後嘗試找到.altcontent第一.content內,這可能不是你想要的。您可以使用.end()回到以前的jQuery對象
  • 你被$之後插入更新的行(「#moduleRow」),它應該已經爲原始行($('#moduleRow').next()

後插入
var currentRow = $("#moduleRow").next(); 
var rowToBeAdded = currentRow.clone() 
    .find(".content:not(:first),.altcontent:not(:first)").remove().end() 
    .insertAfter(currentRow); 

http://jsfiddle.net/orig/rL5aZ/4/

編輯

其實也可以是簡單的:

var currentRow = $("#moduleRow").next(); 
currentRow.clone().insertAfter(currentRow) 
    .find(".content:not(:first),.altcontent:not(:first)").remove(); 
+0

謝謝您的回答,它完美的作品中的jsfiddle但出於某種原因,在我的應用中刪除所有。內容和altcontent行和插入空行。我附上了html代碼的截圖。 – azamsharp 2012-02-11 17:38:50

+0

應該是'rowToBeAdded.clone()。insertAfter(rowToBeAdded)' – ori 2012-02-11 17:44:47

+0

謝謝我改變了代碼,但由於某些原因tBody總是空的新添加的行!似乎它由於某種原因刪除了所有的行。 – azamsharp 2012-02-11 17:51:21