2014-02-19 30 views
2
<table id="mytable"> 

<tr id="gonnaclone"> 
    <td> 
     <input type="text" id ="isim" name="f1" class="large" value = "" /> 
    </td> 
    <td> 
     <input type="checkbox" id ="komut" name="f2" checked/> 
    </td> 
</tr> 

</table> 

我試圖克隆錶行,但它與克隆行的值相同的到來。複製錶行,而不重視

var table = document.getElementById("mytable"); 
var row = document.getElementById("gonnaclone"); 
var clone = row.cloneNode(true); 
table.appendChild(clone); 

我試過row.cloneNode(false);但它停止工作。

如何使克隆行的值爲空?

+0

具有IDS你爲什麼要克隆的元素? – Hrishi

+0

默認情況下深克隆參數爲false,它如何停止工作? 我有點不確定你的意思是使克隆行的值爲false。 你的意思是克隆它,但不是它的孩子​​的? – ioseph

+0

@Hrishi,因爲我找不到任何其他方法。 – abby

回答

3

使用jQuery,你可以做到以下幾點:

$("table").append($("table") 
    .find("#gonnaclone").clone() 
    .find("input").val("").end()); 

什麼這實際上做的是做一個克隆,發現輸入字段,復位輸入值,並將其添加到表中。

但令我更清楚,你也應該刪除克隆行的ID(否則你將不得不重複的ID):

$("table").append($("table") 
    .find("#gonnaclone").clone().removeAttr("id") 
    .find("input").val("").end()); 

一個完整的例子可以在JSFiddle找到。


如果你也想迫使複選框處於某種狀態(選中/取消),你甚至可以使一個更大鏈:

$("table").append($("table") 
    .find("#gonnaclone").clone().removeAttr("id") 
    .find("input").val("").end() 
    .find("input:checked").attr("checked", false).end()); 

這實際上取消選中克隆行。

+0

正是我想要的。謝謝 – abby

4

你可以這樣做

  var table = document.getElementById("mytable"); 
     var row = document.getElementById("gonnaclone"); 
     var clone = row.cloneNode(true); 

     /** 
      Will Ensure that blank entry are appended in html 
     **/ 
     var InputType = clone.getElementsByTagName("input"); 

     for (var i=0; i<InputType.length; i++){ 
     if(InputType[i].type=='checkbox'){ 
      InputType[i].checked = false; 
     }else{ 
      InputType[i].value='';    
      } 
     } 
     table.appendChild(clone); 
+0

請求任何查詢 – sanjeev

+0

是的,它正在工作,但它沒有設置複選框的值 – abby

+0

@abby,做複選框也 – sanjeev