提交表单之前该怎么做?
例如,我有一个带有多个输入按钮的 HTML 表单,
<form action="/index.php" method="post">
<input type="hidden" name="hf" />
<input type="submit" id="apply_action" name="action" value="Apply">
<input type="submit" id="cancel_action" name="action" value="Cancel">
它是一个大型 Web 项目的当前框架。一旦点击“提交”按钮“应用”,它将映射到Web服务器上的特定PHP函数,然后返回特定的HTML代码;当然,一旦点击“取消”,它将映射到另一个 PHP 函数。
现在,我想在单击“应用”时在原始提交操作之前添加一些内容。例如:我想在原始提交操作发送到服务器之前添加一个 javascript 警报(“hello”)?我不知道如何用 Jquery 实现这一点?首先,这个表单有两个提交动作,value='Apply'或'Cancel',我需要区分动作; 其次,在alert('hello')之后,我仍然需要执行原始表单action='index.php'。 有没有用于此目的的 jquery 函数?谢谢!
For example, I have a HTML form with several input buttons,
<form action="/index.php" method="post">
<input type="hidden" name="hf" />
<input type="submit" id="apply_action" name="action" value="Apply">
<input type="submit" id="cancel_action" name="action" value="Cancel">
it's the current framework of a large web project. Once Submit button 'Apply' is clicked, it will map to a particular PHP function on the web server, and then return certain HTML code back; of course, once 'Cancel' is clicked, it will map to another PHP function.
Now, I would like to add some stuff before the original submit action when the 'Apply' is clicked. For example: I would like to add an javascript alert ("hello") before the original submit action is sent to the server? I dont know how to achieve this with Jquery? First of all, there are two submit actions for this form, value='Apply' or 'Cancel', I need to differentiate the action;
Second, after alert('hello'), I still need the original form action='index.php' to be executed.
Is there a jquery function for this purpose? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您希望将一个函数绑定到表单的
submit
事件。请参阅 http://api.jquery.com/submit/为了满足您的请求,这应该可以工作:
附录:
好的,由于您有两个提交按钮,因此您需要使用
click
事件。无论采用哪种方法,表单都将在单击后提交。正如 Francisco Soto 所指出的,返回 false 将阻止通过任一方法提交。返回 true(或不指定返回值)将继续表单提交。
It sounds like you wish to bind a function to the form's
submit
event. See http://api.jquery.com/submit/To fit your request, this should work:
Addendum:
Okay, since you have two submit buttons, you'll need to use the
click
event.With either method, the form will be submitted on click. As noted by Francisco Soto, returning false will prevent the submission from either method. Returning true (or not specifying a return value) will continue with the form submission.
您应该对按钮使用
.click()
方法。You should use the
.click()
method for the button.