Disabling enter key for form

后端 未结 12 788
深忆病人
深忆病人 2020-12-04 21:07

I have been trying to disable the Enter key on my form. The code that I have is shown below. For some reason the enter key is still triggering the submit. The cod

相关标签:
12条回答
  • 2020-12-04 21:41

    Just add following code in <Head> Tag in your HTML Code. It will Form submission on Enter Key For all fields on form.

    <script type="text/javascript">
        function stopEnterKey(evt) {
            var evt = (evt) ? evt : ((event) ? event : null);
            var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
            if ((evt.keyCode == 13) && (node.type == "text")) { return false; }
        }
        document.onkeypress = stopEnterKey;
    </script>
    
    0 讨论(0)
  • 2020-12-04 21:42

    In your form tag just paste this:

    onkeypress="return event.keyCode != 13;"
    

    Example

    <input type="text" class="search" placeholder="search" onkeypress="return event.keyCode != 13;">
    

    This can be useful if you want to do search when typing and ignoring ENTER.

    /// Grab the search term
    const searchInput = document.querySelector('.search')
    /// Update search term when typing
    searchInput.addEventListener('keyup', displayMatches)
    
    0 讨论(0)
  • 2020-12-04 21:52

    For a non-javascript solution, try putting a <button disabled>Submit</button> into your form, positioned before any other submit buttons/inputs. I suggest immediately after the <form> opening tag (and using CSS to hide it, accesskey='-1' to get it out of the tab sequence, etc)

    AFAICT, user agents look for the first submit button when ENTER is hit in an input, and if that button is disabled will then stop looking for another.

    A form element's default button is the first submit button in tree order whose form owner is that form element.

    If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behavior must cause the user agent to run synthetic click activation steps on that default button.

    Consequently, if the default button is disabled, the form is not submitted when such an implicit submission mechanism is used. (A button has no activation behavior when disabled.)

    https://www.w3.org/TR/html5/forms.html#implicit-submission

    However, I do know that Safari 10 MacOS misbehaves here, submitting the form even if the default button is disabled.

    So, if you can assume javascript, insert <button onclick="return false;">Submit</button> instead. On ENTER, the onclick handler will get called, and since it returns false the submission process stops. Browsers I've tested this with won't even do the browser-validation thing (focussing the first invalid form control, displaying an error message, etc).

    0 讨论(0)
  • 2020-12-04 21:54

    Here's a simple way to accomplish this with jQuery that limits it to the appropriate input elements:

    //prevent submission of forms when pressing Enter key in a text input
    $(document).on('keypress', ':input:not(textarea):not([type=submit])', function (e) {
        if (e.which == 13) e.preventDefault();
    });
    

    Thanks to this answer: https://stackoverflow.com/a/1977126/560114.

    0 讨论(0)
  • 2020-12-04 21:55

    Most of the answers are in jquery. You can do this perfectly in pure Javascript, simple and no library required. Here it is:

    <script type="text/javascript">
    window.addEventListener('keydown',function(e){if(e.keyIdentifier=='U+000A'||e.keyIdentifier=='Enter'||e.keyCode==13){if(e.target.nodeName=='INPUT'&&e.target.type=='text'){e.preventDefault();return false;}}},true);
    </script>
    

    This code works great because, it only disables the "Enter" keypress action for input type='text'. This means visitors are still able to use "Enter" key in textarea and across all of the web page. They will still be able to submit the form by going to the "Submit" button with "Tab" keys and hitting "Enter".

    Here are some highlights:

    1. It is in pure javascript (no library required).
    2. Not only it checks the key pressed, it confirms if the "Enter" is hit on the input type='text' form element. (Which causes the most faulty form submits
    3. Together with the above, user can use "Enter" key anywhere else.
    4. It is short, clean, fast and straight to the point.

    If you want to disable "Enter" for other actions as well, you can add console.log(e); for your your test purposes, and hit F12 in chrome, go to "console" tab and hit "backspace" on the page and look inside it to see what values are returned, then you can target all of those parameters to further enhance the code above to suit your needs for "e.target.nodeName", "e.target.type" and many more...

    0 讨论(0)
  • 2020-12-04 21:55

    try this ^^

    $(document).ready(function() {
            $("form").bind("keypress", function(e) {
                if (e.keyCode == 13) {
                    return false;
                }
            });
        });
    

    Hope this helps

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