如何使用jQuery复制和清除表单元素?

如何使用jQuery复制和清除表单元素?,jquery,forms,clone,Jquery,Forms,Clone,如果表单中包含以下html: <div class='example'> <div class='row'> <input name='first_field' type='text' value='Hello' /> <input name='second_field' type='text' value='World!' /> </div> </div> 但这不起作用,因为它似乎

如果表单中包含以下html:

<div class='example'>
   <div class='row'>
       <input name='first_field' type='text' value='Hello' />
       <input name='second_field' type='text' value='World!' />
   </div>
</div>
但这不起作用,因为它似乎不删除值,也不捕获外部html(包装器)

有人能提供建议吗


谢谢。

您的js中有语法错误
.find('.row:first child)
应该是
.find('.row:first child')
,但对于克隆,默认行为是去掉值。划伤那个。。。显然,这只是为了克隆实际的输入元素

你是不是少了一个“后”字。排:第一个孩子“?
   <div class='row'>
       <input name='first_field' type='text' value='' />
       <input name='second_field' type='text' value='' />
   </div>
var row = $('.example').find('.row:first-child').clone(); 
row.find('[name]').each(function() {
  $(this).val('');
});
var row = $('.example').find('.row:first-child').clone();  
row.find('[name]').each(function() {
  $(this).val('');
});
$(".row:last").after(row)