Submitting a form on 'Enter' with jQuery?

后端 未结 14 1634
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 14:45

I have a bog-standard login form - an email text field, a password field and a submit button on an AIR project that\'s using HTML/jQuery. When I hit Enter on the form, the

相关标签:
14条回答
  • 2020-11-22 15:13

    Don't know if it will help, but you can try simulating a submit button click, instead of directly submitting the form. I have the following code in production, and it works fine:

        $('.input').keypress(function(e) {
            if(e.which == 13) {
                jQuery(this).blur();
                jQuery('#submit').focus().click();
            }
        });
    

    Note: jQuery('#submit').focus() makes the button animate when enter is pressed.

    0 讨论(0)
  • 2020-11-22 15:13

    Also to maintain accessibility, you should use this to determine your keycode:

    c = e.which ? e.which : e.keyCode;
    
    if (c == 13) ...
    
    0 讨论(0)
  • 2020-11-22 15:14

    I found out today the keypress event is not fired when hitting the Enter key, so you might want to switch to keydown() or keyup() instead.

    My test script:

            $('.module input').keydown(function (e) {
                var keyCode = e.which;
                console.log("keydown ("+keyCode+")")
                if (keyCode == 13) {
                    console.log("enter");
                    return false;
                }
            });
            $('.module input').keyup(function (e) {
                var keyCode = e.which;
                console.log("keyup ("+keyCode+")")
                if (keyCode == 13) {
                    console.log("enter");
                    return false;
                }
            });
            $('.module input').keypress(function (e) {
                var keyCode = e.which;
                console.log("keypress ("+keyCode+")");
                if (keyCode == 13) {
                    console.log("Enter");
                    return false;
                }
            });
    

    The output in the console when typing "A Enter B" on the keyboard:

    keydown (65)
    keypress (97)
    keyup (65)
    
    keydown (13)
    enter
    keyup (13)
    enter
    
    keydown (66)
    keypress (98)
    keyup (66)
    

    You see in the second sequence the 'keypress' is missing, but keydown and keyup register code '13' as being pressed/released. As per jQuery documentation on the function keypress():

    Note: as the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.
    

    Tested on IE11 and FF61 on Server 2012 R2

    0 讨论(0)
  • 2020-11-22 15:18

    Return false to prevent the keystroke from continuing.

    0 讨论(0)
  • 2020-11-22 15:23

    Is there any reason you have to hook and test for the enter key?

    Couldn't you simply add a

    <input type="submit" /> 
    

    to your form and have it naturally be submitted when enter is pushed? You could even then hook the form's onsubmit action and call a validation function from there if you wanted...

    You could even use the onsubmit as a test to see if your form is being submitted, but it won't work if you call form.submit().

    0 讨论(0)
  • 2020-11-22 15:26

    I use now

    $("form").submit(function(event){
    ...
    }
    

    At first I added an eventhandler to the submit button which produced an error for me.

    0 讨论(0)
提交回复
热议问题