题
这似乎相对简单,我只是在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”更改为“类”,因为ID必须在每个页面上是唯一的。更多有关.append()官方文档中的append()
不隶属于 StackOverflow