2011-11-21 110 views
0

我想克隆和重命名ID和名稱,但下面的代碼似乎並沒有工作。如果有任何建議,請讓我知道嗎?克隆和重命名ID和名稱

它看起來像下面的行有問題。

cloned.id = what.attr('id')+「_」+ cur_num;

這是我的功能。

 var cur_num = 1; // Counter 
    $("#btnClone").click(function(){ 

      var what = $("#MainConfig"); 
      var where = $("#smallConfig"); 

      var cloned = what.clone(true, true).get(0); 
      ++cur_num; 
      cloned.id = what.attr('id') + "_" + cur_num;     // Change the div itself. 

     $(cloned).find("*").each(function(index, element) { // And all inner elements. 
      if(element.id) 
      { 
       var matches = element.id.match(/(.+)_\d+/); 
       if(matches && matches.length >= 2)    
        element.id = matches[1] + "_" + cur_num; 
      } 
      if(element.name) 
      { 
       var matches = element.name.match(/(.+)_\d+/); 
       if(matches && matches.length >= 2)    
        element.name = matches[1] + "_" + cur_num; 
      } 

     }); 

     $(cloned).appendTo(where); 

    }); 
}); 
+1

你似乎對jQuery集合對象和實際的DOM對象有些困惑。 –

回答

0

要使用jQuery的功能,您需要使用適當的語法引用「what」變量。你應該使用「$(what)」而不是「what」。

0

嘗試將ID保存在其他變量中。您正嘗試將ID保存到jQuery對象中。