在jQuery中克隆表单并增加索引

这看起来相对简单,我只是难以理解jQuery语法。 基本上我想采用这种形式:
<div class="me_signup">
  <input type="text" name="referral[0][name]" id="referral_0_name">
  <br>
  <input type="text" name="referral[0][email]" id="referral_0_email">
</div>
并用一个按钮复制它并增加变量号..
$(".add_another_button").click(function(){
    ...
};
    
已邀请:
像这样的东西?
$(".add_another_button").click(function() {
    var $newdiv = $(".me_signup:last").clone(true);
    $newdiv.find('input').each(function() {
        var $this = $(this);
        $this.attr('id', $this.attr('id').replace(/_(d+)_/, function($0, $1) {
            return '_' + (+$1 + 1) + '_';
        }));
        $this.attr('name', $this.attr('name').replace(/[(d+)]/, function($0, $1) {
            return '[' + (+$1 + 1) + ']';
        }));
        $this.val('');
    });
    $newdiv.insertAfter('.me_signup:last');
});
    
您可以将所有html放在变量中,并使用.append()方法将其添加到特定的DOM元素中。并且,如果您克隆它,请确保将“id”更改为“class”,因为id在每个页面上必须是唯一的。 有关官方文档中的.append()的更多信息     

要回复问题请先登录注册