简体   繁体   中英

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). In the following example "now here" keeps showing up after the form is valid (that is all fields have some text).

<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? 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?

UPDATE: I found it here in Bugzilla. You can vote for it . It's been reported since late 2001, why has it bean so long and not fixed???

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.

My jsfiddle ( http://jsfiddle.net/BhaeG/ ) only fires the alert("now here") when all conditions of the if statement are correct.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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