简体   繁体   English

为什么这个事件会被解雇?

[英]Why does this event keep getting fired?

This script checks to see if the the form has been filled in correctly but the toggle() function keeps getting fired (at least in FireFox). 此脚本检查表单是否已正确填写,但toggle()函数一直被触发(至少在FireFox中)。 In the following example "now here" keeps showing up after the form is valid (that is all fields have some text). 在下面的示例中,“now here”在表单有效后继续显示(即所有字段都有一些文本)。

<script type="text/javascript">
        <!--
        function toggle()
        {
            if(document.getElementById('username').value.length > 1 && document.getElementById('pw1').value.length > 1 && document.getElementById('pw2').value.length > 1 && (document.getElementById('pw1').value == document.getElementById('pw2').value))
            {
                alert("now here")
                document.getElementById('submit').disabled = false
            }
        }
//-->
</scipt>
Username:<input type="text" name='username' id='username' maxlength="30" onkeyup="toggle()"/><br />
        Password:<input type="password" name="pw1" id="pw1" onkeyup="passwordCheck(document.getElementById('pw1'), document.getElementById('pw2')); toggle()"/><br />
        Confirm password:<input type="password" name="pw2" id="pw2" onkeyup="passwordCheck(document.getElementById('pw1'), document.getElementById('pw2')); toggle()"/>

I'm woundering why does onkeyup keep getting fired? 我想知道为什么onkeyup被解雇? It's as if the user never takes there finger off the key. 好像用户永远不会把手指放在钥匙上。 Is it because onkeyup gets screwed up when it tirggers an alert window because the focus changes from the text field to the window and when ok is pressed the focus changes back to the text field? 是因为onkeyup因为焦点从文本字段变为窗口而变得紧张,因为焦点从文本字段变为窗口,当按下确定时焦点会变回文本字段?

UPDATE: I found it here in Bugzilla. 更新:我在Bugzilla 找到了它 You can vote for it . 你可以投票支持它 It's been reported since late 2001, why has it bean so long and not fixed??? 自2001年底以来一直有报道,它为什么豆这么长而且不固定?

Looks like you want the function to make the submit change from disabled to enabled when the form is correctly filled out. 看起来您希望该函数在正确填写表单时将提交更改从禁用更改为启用。 The function should fire every time the onKeyUp is called with the all three inputs because you are calling it in all three inputs. 每次使用全部三个输入调用onKeyUp时,该函数应该触发,因为您在所有三个输入中调用它。

My jsfiddle ( http://jsfiddle.net/BhaeG/ ) only fires the alert("now here") when all conditions of the if statement are correct. 我的jsfiddle( http://jsfiddle.net/BhaeG/ )只在if语句的所有条件都正确时触发alert("now here")

Unless I'm really missing something, it seems like it's working as designed. 除非我真的错过了一些东西,否则它似乎正在按设计工作。

在FireFox中,如果您在警报窗口按Enter键,它将触发onKeyUp

I got around this issue by using onkeydown instead of onkeyup. 我通过使用onkeydown而不是onkeyup解决了这个问题。 YMMV. 因人而异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM