两种形式一个提交按钮
问题描述:
我有两种形式,我想同时使用两种形式。我知道可以使用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>
窗体2
<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>
注:
我想运行Form1中为Javscript
-
我想运行form2提交按钮作为onclick。
有人吗?
答
试试这个:
function conti(){
$("form").first().submit();
}
+1
提供解释和解释。 –
答
给表格2的ID,并为此创造形式的JS提交事件处理程序,将序列化表单数据和发送它的形式行动,功能将被称为形式2提交按钮,我们正在做的是防止默认提交行动,并提交我们的形式th粗略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);
}
});
});
表单1将通过自己的提交按钮提交默认行为。
如果你有一个名为“submit”的表单控件,那么你不能调用表单的提交方法(因为* form.submit *现在引用了控件,而不是方法)。您的markkup还有其他问题。 – RobG
[* MDN *](https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started)介绍了普通JS XMLHttpRequest的基础知识。这真的不难,并且有很多小型图书馆可以提供帮助。 – RobG