简体   繁体   English

jQuery-如何提交克隆的表单?

[英]jQuery - How do I submit the cloned forms?

How can I submit all the cloned forms at the same time? 如何同时提交所有克隆的表格?

I made a script to clone my form, and I want to submit all the cloned forms. 我制作了一个脚本来克隆表单,我想提交所有克隆的表单。 How can I do that? 我怎样才能做到这一点?

HTML: 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 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());
}); 

Submitting a form is triggering request for new page load, thus you can't submit several forms simultaenously. 提交表单会触发新页面加载的请求,因此您不能同时提交多个表单。 Try to collect all forms' values in a hidden form to be submitted or use some AJAX to do the job without actually submitting form data. 尝试以隐藏的形式收集要提交的所有表单的值,或者使用某些AJAX来完成工作,而无需实际提交表单数据。

Alternatively you could clone your form's content to extend the form itself. 或者,您可以克隆表单的内容以扩展表单本身。

<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>

Your JS might look like this: 您的JS可能如下所示:

 $("#extend").click( function() {
      $(".cloneable")
          .clone()
          .insertBefore( $("#extend") );
 } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM