如何在提交按钮 onclick 事件中取消表单提交?
我正在开发 ASP.net Web 应用程序。
我有一个带有提交按钮的表单。提交按钮的代码类似于 。
我想写如下内容:
function btnClick() {
if (!validData())
cancelFormSubmission();
}
我该怎么做?
I'm working on an ASP.net web application.
I have a form with a submit button. The code for the submit button looks like <input type='submit' value='submit request' onclick='btnClick();'>
.
I want to write something like the following:
function btnClick() {
if (!validData())
cancelFormSubmission();
}
How do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
您最好这样做...
如果
isValidForm()
返回false
,则您的表单不会提交。您可能还应该将事件处理程序从内联中移出。
You are better off doing...
If
isValidForm()
returnsfalse
, then your form doesn't submit.You should also probably move your event handler from inline.
将您的输入更改为:
并在函数中返回 false
Change your input to this:
And return false in your function
您需要更改
为
和
就是说
,我会尽量避免使用内部事件属性,以支持 unobtrusive JS 使用一个库(例如 YUI 或 jQuery),该库具有良好的事件处理 API 并与真正重要的事件相关联(即表单的提交事件而不是按钮的单击事件)。
You need to change
to
and
to
That said, I'd try to avoid the intrinsic event attributes in favour of unobtrusive JS with a library (such as YUI or jQuery) that has a good event handling API and tie into the event that really matters (i.e. the form's submit event instead of the button's click event).
有时
onsubmit
不适用于 ASP.NET。我用非常简单的方法解决了它。
如果我们有这样一个表单,
您现在可以在表单回发之前捕获该事件并停止回发,并使用此 jquery 执行您想要的所有 ajax。
Sometimes
onsubmit
wouldn't work with asp.net.I solved it with very easy way.
if we have such a form
You can now catch get that event before the form postback and stop it from postback and do all the ajax you want using this jquery.
您应该将类型从
submit
更改为button
:而不是
在 JavaScript 中获取按钮的名称,并将您想要的任何操作与它对
我有用的操作 关联起来
希望有帮助。
you should change the type from
submit
tobutton
:instead of
you then get the name of your button in javascript and associate whatever action you want to it
worked for me
hope it helps.
这是一个非常古老的线程,但肯定会引起注意。因此,请注意,所提供的解决方案不再是最新的,现代 Javascript 更好。
该表单接收一个监视提交的事件处理程序。如果调用的函数 validData(此处未显示)返回 FALSE,则调用方法 PreventDefault,该方法会抑制表单的提交,并且浏览器返回到输入。否则,表格将照常发送。
PS 这也适用于属性onsubmit。那么匿名函数 function(event){...} 必须位于表单的属性 onsubmit 中。这并不是真正的现代,您只能使用一个事件处理程序来进行提交。但您不必创建额外的 javascript。另外,它可以直接在源代码中指定为表单的属性,而无需等到表单集成到 DOM 中。
This is a very old thread but it is sure to be noticed. Hence the note that the solutions offered are no longer up to date and that modern Javascript is much better.
The form receives an event handler that monitors the submit. If the there called function validData (not shown here) returns a FALSE, calling the method PreventDefault, which suppresses the submit of the form and the browser returns to the input. Otherwise the form will be sent as usual.
P.S. This also works with the attribute onsubmit. Then the anonymus function function(event){...} must in the attribute onsubmit of the form. This is not really modern and you can only work with one event handler for submit. But you don't have to create an extra javascript. In addition, it can be specified directly in the source code as an attribute of the form and there is no need to wait until the form is integrated in the DOM.
您需要
返回 false;
:You need to
return false;
:使用 JQuery 更加简单:适用于 Asp.Net MVC 和 Asp.Core
With JQuery is even more simple: works in Asp.Net MVC and Asp.Core
为什么不将提交按钮更改为常规按钮,并在单击事件中,如果通过验证测试则提交表单?
例如
Why not change the submit button to a regular button, and on the click event, submit your form if it passes your validation tests?
e.g
您需要
onSubmit
。不是onClick
否则有人只需按 Enter 键即可绕过您的验证。至于取消。你需要返回 false。代码如下:Edit onSubmit 属于表单标记。
You need
onSubmit
. NotonClick
otherwise someone can just press enter and it will bypass your validation. As for canceling. you need to return false. Here's the code:Edit onSubmit belongs in the form tag.
很简单,返回false即可;
下面的代码使用 jquery 在提交按钮的 onclick 中进行。
It's simple, just return false;
The below code goes within the onclick of the submit button using jquery..
使用
onclick='return btnClick();'
和
use
onclick='return btnClick();'
and