使用jquery克隆表单并删除属性

时间:2013-09-04 13:20:05

标签: javascript jquery

是否可以在不影响功能的情况下克隆表单? 实际上,表单的id将是重复的。禁止具有相同id的两个DOM对象。

<form id='originalForm' name='originalForm' enctype='multipart/form-data' action=''>  
   <input id='Firstname' name='firstname' type='text'/>  
   <input id='Lastname' name='lastname' type='text' />  
   <input id='photo' name='photo' type='file' />
</form>  
...  
var copyForm = jQuery('#originalForm').clone();  
var e = copyForm.find(':input').not(':file');
e.each(function()  
{
  jQuery($this).removeAttr('name');
}

1 个答案:

答案 0 :(得分:1)

var originalForm = jQuery('#originalForm');
//clone the form
var newForm = originalForm.clone(); 
//change form ID
newForm.attr('id', 'newID'); 
//remove `name` attr from all non file inputs
newForm.find('input:not(:file)').removeAttr('name'); 
//add new form to the page after the old one
originalForm.after(newForm);
相关问题