2012-02-11 104 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