2011-08-30 25 views
2

單擊某一行中的複製鏈接時,我需要創建另一行並將其所有內容複製到新行。這是我有的代碼:無法複製單元值

addNewInlineRow(sid, idToUse); // this creates a new row with empty values 
//this copies the value from old row to new row 
$("#"+rowId+ " td").each(function(index){ 
    $("#"+idToUse+ " td").get(index).text($(this).text()); 
}); 

但是這段代碼不起作用。有什麼建議麼?由於存在用於創建TR ID的複雜邏輯,我無法克隆整個TR!

+0

你試過克隆TR? –

回答

0

當您使用.get()您正在檢索DOM元素時,它不是jQuery對象,因此您不能使用.text()來設置文本值。嘗試:

$("#"+idToUse+ " td").get(index).innerHTML = $(this).text(); 
-1

當你想檢索值形成像div,span

所有容器必須在jQuery的使用.html()

嘗試打擊代碼:

$("#"+idToUse+ " td").get(index).html($(this).html()); 
1

您可以使用clone方法得到的tr克隆並追加到表TBODY。

試試這個

function addNewInlineRow(sid, idToUse){ 
    var $clone = $("#"+sid).clone(); 
    $clone.attr("id", idToUse); 

    //Now append the clone to table 
    $("table").append($clone); 
} 
+0

你打敗了我,因爲我慢了一把小提琴。這裏是任何人通過這個頁面停止:http://jsfiddle.net/gZhdg/ –

0

您仍然可以克隆整個TR,然後更新需要更新(即ID)的部分:

function addNewInlineRow(sid, idToUse){ 
    $('#' + sid).clone().attr('id', idToUse).appendTo('table'); 
}