表单'onsubmit'没有被调用 [英] Form 'onsubmit' not getting called

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

问题描述

下面是我的表单部分:

 < form name ='form-main'onsubmit ='return validate( )'action =''method ='post'> 
< center>< input type ='submit'onClick ='this.disabled = true; this.form.submit();'value ='我接受 - 下载GM!'/>< / center>
< / form>

以下是 validate 函数: p>

 函数validate()
{
//这只是为了测试它是否真的显示
警报('你不能留下任何空白字段!');
返回false;
}

无论何时我点击提交按钮,什么都不会发生,页面只是重新加载。我希望它能显示警告对话框。

解决方案

当您调用表单的 submit 函数, submit 事件不会被触发。这是设计的,假设是如果你触发代码提交,你已经做了任何必要的验证。 (请注意,对于> HTMLFormElement#submit 函数来说是这样的;它并不一定适用于包装库。)

>

在你的例子中,我将删除按钮上的点击处理程序。这是一个提交按钮,因此只需在窗体上的 submit 事件中添加任何相关逻辑即可。或者,如果您愿意,请将 validate()作为按钮的单击的一部分。

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>

and here's the validate function:

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.

解决方案

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.)

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.

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

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