简体   繁体   中英

Javascript form submission when pressing enter not working in Firefox with saved password

I have a simple login form that captures username/password with some simple Javascript (JQuery) to submit the form when the user presses enter in either of those fields. Here's the relevant bits of code:

The form:

<form id="loginForm">
    <input type="text" id="username" />
    <input type="password" id="password" />
    etc...
</form>

The Javascript:

$('#username, #password').bind('keypress', function(e) {
    if (e.which == 13) {
        $('#loginForm').submit();
        e.preventDefault();
    }
});

The problem I'm having is specific to Firefox when the user has turned on the feature to have the browser save their passwords.

If I type the first few letters of my username in the username text field Firefox will present me with the options that it has remembered. If I press the down arrow to highlight the username and then hit the enter key to select it then my Javascript is firing before Firefox auto-completes the password field. This results in a blank password being submitted.

It works fine when using the same 'save passwords' feature in Chrome.

Any ideas on how to work around this?

Changed keypress to keyup and all it works perfectly.

$('#username, #password').bind('keyup', function(e) {
    if (e.which == 13) {
        $('#loginForm').submit();
        e.preventDefault();
    }
});

Thanks CoolEsh for the tip

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