Javascript onchange事件阻止HTML表单中的onsubmit事件? [英] Javascript onchange event preventing onsubmit event in HTML form?

查看:329
本文介绍了Javascript onchange事件阻止HTML表单中的onsubmit事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form action =method =POSTonsubmit =return checkValidArray( )> 
< input type =textname =dataonchange =return validate(this);>
< / form>

看来(在IE 6和Firefox 3中)当您在输入字段中键入一些文本然后单击提交,onchange事件触发输入字段,但onsubmit事件不会为表单触发,直到您再次点击提交按钮(当然onchange不会触发)。换句话说,onchange似乎可以防止onsubmit被触发。



当用户退出字段时,需要检查表单中的每个字段以提供并提供即时验证反馈,并防止在无效数据时提交表单。





如何解决这个问题?解决方案(

一种排序):

事实证明,在输入的onchange事件期间,只有alert()或者confirm() - 会导致表单的onsubmit事件不开火。 JS线程似乎被alert()阻塞。

解决方法是在输入的onchange调用中不包含alert()或confirm()。



已知受影响的浏览器:

IE6 - Windows

IE8 - Win

FF3 - Win

已知不受影响的浏览器


Google Chrome - 赢得
Safari - Mac

FF - Mac


Consider an HTML form:

<form action="" method="POST" onsubmit="return checkValidArray()">
    <input type="text" name="data" onchange="return validate(this);">
</form>

It appears (in both IE 6 and Firefox 3) that when you type some text into the input field and click submit that the onchange event fires for the input field, but the onsubmit event does not fire for the form until you click the submit button a second time (at which time the onchange does not fire, of course). In other words, the onchange seems to prevent the onsubmit from firing.

The desire is to check each field in the form when the user exits the field in order to provide them with immediate validation feedback, and also to prevent submission of the form if there is invalid data.

[EDIT: Note that both validation functions contain an alert().]

How does one get around this problem?

解决方案

Solution (of a sort):

It turns out that it is only presence of an alert() - or a confirm() - during the input's onchange event that causes the form's onsubmit event to not fire. The JS thread appears to get blocked by the alert().
The workaround is to not include any alert() or confirm() in the input's onchange call.

Browsers known to be affected:
IE6 - Windows
IE8 - Win
FF3 - Win

Browsers known not to be affected:
Google Chrome - Win
Safari - Mac
FF - Mac

这篇关于Javascript onchange事件阻止HTML表单中的onsubmit事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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