如何同时提交所有克隆表单?
我制作了一个脚本来克隆我的表单,并且我想提交所有克隆的表单。我怎样才能做到这一点?
HTML:
<div id='forms'>
<div class='cform'>
<form id='form' method='POST'>
<input style='width: 80px;' class='hihi' type='submit' name='add_jo' value='Submit all' />
<td><input class='txtedit' placeholder='Job name' type='text' name='jo[]' maxlength='130' /></td>
</form>
</div>
</div>
jQuery
$('.clone').click(function(event) {
event.preventDefault();
var tr = $('.cform:first');
var newTr = tr.clone();
newTr.find(":input").val(''); // find all input types (input, textarea etc), empty it.
newTr.appendTo(tr.parent());
});
最佳答案
提交表单会触发新页面加载请求,因此您不能同时提交多个表单。尝试在要提交的隐藏表单中收集所有表单的值,或者使用一些 AJAX 来完成这项工作,而不实际提交表单数据。
或者,您可以克隆表单的内容以扩展表单本身。
<form action="..." method="post">
<div class="cloneable">
<input name="data[]" value="" />
</div>
<button id="extend">Clone</button>
<button type="submit" id="submit">Submit</button>
</form>
你的 JS 可能看起来像这样:
$("#extend").click( function() {
$(".cloneable")
.clone()
.insertBefore( $("#extend") );
} );
关于javascript - jQuery - 如何提交克隆表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23199363/