未调用表单“提交" [英] Form 'onsubmit' not getting called

查看:29
本文介绍了未调用表单“提交"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的表单的一部分:

Here's the part of my form:

<form name='form-main' onsubmit='return validate()' action='' method='post'>
    <center><input type='submit' onClick='this.disabled=true; this.form.submit();' value='I accept - Download the GM!'/></center>
</form>

这里是 validate 函数:

function validate()
{
    // this is just to test if it actually shows
    alert('You must not leave any of the fields blank!');
    return false;
}

每当我点击提交按钮时,什么都没有发生,页面只是重新加载..我希望它显示警报对话框.

Whenever I hit the submit button, nothing happens, the page just reloads.. I would like it so it shows the alert dialog.

推荐答案

当您调用表单的 submit 函数时,不会触发 submit 事件.这是 设计,假设是如果你从代码触发提交,你'已经完成了任何必要的验证.(请注意,这对于 HTMLFormElement#submit 函数来说是正确的;对于围绕它的包装器库来说,它不一定是正确的.)

When you call the form's submit function, the submit event is not fired. This is by design, the assumption is that if you're triggering the submission from code, you've already done any necessary validation. (Note that this is true of the HTMLFormElement#submit function; it is not necessarily true of the wrappers libraries put around it.)

在您的示例中,我将删除按钮上的 click 处理程序.它是一个提交按钮,因此只需将任何相关逻辑放在表单上的 submit 事件中.或者,如果您愿意,可以在按钮的 click 中调用 validate().

In your example, I would remove the click handler on the button. It's a submit button, so just put any relevant logic in the submit event on the form. Alternately, if you prefer, call validate() as part of the button's click.

这篇关于未调用表单“提交"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆