扩展克隆表行function – 更改行ID

我有这个小提琴: http : //jsfiddle.net/radi8/EwQUW/33/

请注意,初始表定义为:

当我克隆第一行时,我需要将新行的ID更改为类似的内容

 

其中number是行的新ID。

有人可以指导我如何做到这一点?

也许我错过了一些东西,但这应该像设置ID属性一样简单:

 var $clone = $("#template").clone(); var index = $("table.reference tr:not(.template)").length; $clone.attr("id", "emlRow_" + index); 

更新

var count = $(“table.reference tr”)。length;

 $("#Add").click(function() { count++; var $clone = $("#secondaryEmails tbody tr:first").clone(); $clone.attr({ id: "emlRow_" + count, name: "emlRow_" + count, style: "" // remove "display:none" }); $clone.find("input,select").each(function(){ $(this).attr({ id: $(this).attr("id") + count, name: $(this).attr("name") + count }); }); $("#secondaryEmails tbody").append($clone); }); 

工作实例: http : //jsfiddle.net/hunter/EwQUW/35/

你可以尝试这个js代码。

场景: – 我正在使用在div内部克隆表的方案,并在点击删除克隆链接时删除克隆。

   

在这里,我正在更新我的答案,提供删除克隆的代码。

 $(document).ready(function(){ $('.removeClone').live('click',function() { var length=$('.cloneTable').length; if(length==1) { alert('There should be atleast one clone'); return false; } var id = $(this).attr('id'); var countVal=parseInt(id)+parseInt(1); $(this).closest('.cloneTable').remove(); for(var removecount=parseInt(countVal);removecount<=length;removecount++) { $clone=jQuery("#maintable"+removecount); if(removecount==1) { $clone.find(':text,:file').each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr('id',testVal); }); $clone.find("a").each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr('id',testVal+id); }); $clone.find("span").each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr({ id: testVal }); $(this).html('Test '+removecount+''); }); $clone.attr( 'id', function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); return (testVal); }); id=parseInt(id)+parseInt(1); } else { $clone.find(':text,:file').each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr('id',testVal+id); }); $clone.find("a").each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr('id',testVal+id); }); $clone.find("span").each(function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); $(this).attr({ id: testVal+id }); $(this).html('Test '+removecount+''); }); $clone.attr( 'id', function() { var textId=$(this).attr("id"); var testVal=textId.replace(/[0-9]/g, ''); return (testVal+id); }); id=parseInt(id)+parseInt(1); } } count=parseInt(count)-parseInt(1); }); }); 

这项工作对我来说很好。希望这段代码可以帮到你。

SelectRow Email Ship Type
Both Over Road Over Rail
Both Over Road Over Rail