[英]HTML Form Two Different Submit Buttons and Two Different Functions
What is the best way to have an HTML form with two submit buttons and have them both do two different functions without using inline JavaScript? 具有两个提交按钮的HTML表单并使它们都执行两种不同的功能而不使用内联JavaScript的最佳方法是什么? I can use Jquery so doing
$('#id').submit(function (event) {});
我可以使用Jquery这样执行
$('#id').submit(function (event) {});
would be fine. 会没事的。 Thanks.
谢谢。
Don't make them <input type="submit">
, make them <input type="button">
. 不要将它们设置为
<input type="submit">
,将其设置为<input type="button">
。
If you are doing a basic HTTP POST
, then add a click event that will 如果您正在执行基本的
HTTP POST
,则添加一个click事件,该事件将
action
on the form action
submit()
on the form. submit()
。 If you're submitting via Ajax, the click events will just manage the $.post()
process for each button. 如果您通过Ajax提交,则click事件将仅管理每个按钮的
$.post()
流程。
If you are posting the form data to the next page you could detect which button is clicked on, and assign a value to each button such as "button 1" value=1 and "button 2" value= 2. Then execute different functions on the page based on which button is clicked. 如果将表单数据发布到下一页,则可以检测到单击了哪个按钮,并为每个按钮分配一个值,例如“ button 1” value = 1和“ button 2” value =2。然后在单击哪个按钮的页面。 Here is a similar question:
这是一个类似的问题:
How do I use two submit buttons, and differentiate between which one was used to submit the form? 如何使用两个提交按钮,并区分使用哪个按钮提交表单?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.