【问题标题】:Two forms one submit button两个表单一个提交按钮
【发布时间】:2015-07-13 06:04:32
【问题描述】:

我有两个表单,我想同时使用这两个表单。我知道使用 ajax 是可能的。

Form1

<form action="upload.php" method="post">
<label for="url">Enter URL:</label>
<input type="text" name="url" size="35" /><br/>
<label for="filemam">File Name:</label><br/>
<input type="text" name="filenam" size="35" />

<br/> <input type="submit" name="sut" value="Submit" />


</form>

Form2

    <form method="post" action="forum2_add_:user-prvar-7890:.xhtml:admin-hash-amp:"> <div id="bb"><br/> <img src="http://scodec.xtgem.com/400px-Warning_icon.svg_.png" width="20" height="20"/> <b><font color="#696969">Avoid All Capital Letter on your thread title, special character like (') is not allowed.**</font></b><br/> <b>Thread Title:</b> <input type="text" name="tema_nazov" value="" maxlength="200"/></div> <div id="bb"><br/><b><font color="#696969"> <img src="http://scodec.xtgem.com/400px-Warning_icon.svg_.png" width="20" height="20"/> Article content should be easy to read, easy to understand,presentation clear in order to attract readers.</font></b><br/><b>Content:</b> <br/> <textarea name="text" rows="5"></textarea> 





<input type="hidden" name="d_token" value="" /><input type="submit" name="submit" value="Submit" onclick="conti()" style="margin:2px"/></div></form>

</div></div>

注意:

  1. 我想将 form1 作为 Javascript 运行

  2. 我想将 form2 提交按钮作为 onclick 运行。

    有人吗?

【问题讨论】:

  • 如果您有一个名为“submit”的表单控件,那么您不能调用表单的提交方法(因为 form.submit 现在引用的是控件,而不是方法)。您的标记还有其他问题。
  • MDN 很好地介绍了纯 JS XMLHttpRequest 的基础知识。真的不难,还有很多小库可以帮忙。

标签: javascript html ajax forms


【解决方案1】:

给表单 2 一个 id 并为此表单创建一个 js 提交事件处理程序,它将序列化表单数据并将其发送到表单操作,该函数将在表单 2 提交按钮上调用,我们正在做的是防止默认提交操作,并通过 AJAX 提交我们的表单:

将id添加到表格2:

<form method="post" id="formTwo" ....

为表单二创建一个提交函数事件处理程序,调用AJAX函数而不需要表单2的提交按钮上的onclick事件:

$("#formTwo").submit(function (e) {
      e.preventDefault(); //

     form = $(this);
     $.ajax( {
      type: "POST",
      url: form.attr( 'action' ),
      data: form.serialize(),
      success: function( response ) {
        console.log( response );
      }
      } );
});

Form 1 将通过其自己的提交按钮以默认行为提交。

【讨论】:

    【解决方案2】:

    试试这个:

    function conti(){
      $("form").first().submit();
    }
    

    【讨论】:

    • 提供解决方案和解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2016-01-18
    • 2017-01-05
    相关资源
    最近更新 更多