Prevent users from submitting a form by hitting Enter

后端 未结 30 3428
感动是毒
感动是毒 2020-11-21 05:13

I have a survey on a website, and there seems to be some issues with the users hitting enter (I don\'t know why) and accidentally submitting the survey (form) without clicki

相关标签:
30条回答
  • 2020-11-21 05:38

    I'd like to add a little CoffeeScript code (not field tested):

    $ ->
        $(window).bind 'keypress', (event) ->
            if event.keyCode == 13
                unless {'TEXTAREA', 'SELECT'}[event.originalEvent.srcElement.tagName]
                    event.preventDefault()
    

    (I hope you like the nice trick in the unless clause.)

    0 讨论(0)
  • 2020-11-21 05:39

    Disallow enter key anywhere

    If you don't have a <textarea> in your form, then just add the following to your <form>:

    <form ... onkeydown="return event.key != 'Enter';">
    

    Or with jQuery:

    $(document).on("keydown", "form", function(event) { 
        return event.key != "Enter";
    });
    

    This will cause that every key press inside the form will be checked on the key. If it is not Enter, then it will return true and anything continue as usual. If it is Enter, then it will return false and anything will stop immediately, so the form won't be submitted.

    The keydown event is preferred over keyup as the keyup is too late to block form submit. Historically there was also the keypress, but this is deprecated, as is the KeyboardEvent.keyCode. You should use KeyboardEvent.key instead which returns the name of the key being pressed. When Enter is checked, then this would check 13 (normal enter) as well as 108 (numpad enter).

    Note that $(window) as suggested in some other answers instead of $(document) doesn't work for keydown/keyup in IE<=8, so that's not a good choice if you're like to cover those poor users as well.

    Allow enter key on textareas only

    If you have a <textarea> in your form (which of course should accept the Enter key), then add the keydown handler to every individual input element which isn't a <textarea>.

    <input ... onkeydown="return event.key != 'Enter';">
    <select ... onkeydown="return event.key != 'Enter';">
    ...
    

    To reduce boilerplate, this is better to be done with jQuery:

    $(document).on("keydown", ":input:not(textarea)", function(event) {
        return event.key != "Enter";
    });
    

    If you have other event handler functions attached on those input elements, which you'd also like to invoke on enter key for some reason, then only prevent event's default behavior instead of returning false, so it can properly propagate to other handlers.

    $(document).on("keydown", ":input:not(textarea)", function(event) {
        if (event.key == "Enter") {
            event.preventDefault();
        }
    });
    

    Allow enter key on textareas and submit buttons only

    If you'd like to allow enter key on submit buttons <input|button type="submit"> too, then you can always refine the selector as below.

    $(document).on("keydown", ":input:not(textarea):not(:submit)", function(event) {
        // ...
    });
    

    Note that input[type=text] as suggested in some other answers doesn't cover those HTML5 non-text inputs, so that's not a good selector.

    0 讨论(0)
  • 2020-11-21 05:39

    A completely different approach:

    1. The first <button type="submit"> in the form will be activated on pressing Enter.
    2. This is true even if the button is hidden with style="display:none;
    3. The script for that button can return false, which aborts the submission process.
    4. You can still have another <button type=submit> to submit the form. Just return true to cascade the submission.
    5. Pressing Enter while the real submit button is focussed will activate the real submit button.
    6. Pressing Enter inside <textarea> or other form controls will behave as normal.
    7. Pressing Enter inside <input> form controls will trigger the first <button type=submit>, which returns false, and thus nothing happens.

    Thus:

    <form action="...">
      <!-- insert this next line immediately after the <form> opening tag -->
      <button type=submit onclick="return false;" style="display:none;"></button>
    
      <!-- everything else follows as normal -->
      <!-- ... -->
      <button type=submit>Submit</button>
    </form>
    
    0 讨论(0)
  • 2020-11-21 05:40

    It is my solution to reach the goal, it is clean and effective.

    $('form').submit(function () {
      if ($(document.activeElement).attr('type') == 'submit')
         return true;
      else return false;
    });
    
    0 讨论(0)
  • 2020-11-21 05:40
    1. Do not use type="submit" for inputs or buttons.
    2. Use type="button" and use js [Jquery/angular/etc] to submit form to server.
    0 讨论(0)
  • 2020-11-21 05:40

    I had a similiar problem, where I had a grid with "ajax textfields" (Yii CGridView) and just one submit button. Everytime I did a search on a textfield and hit enter the form submitted. I had to do something with the button because it was the only common button between the views (MVC pattern). All I had to do was remove type="submit" and put onclick="document.forms[0].submit()

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