Prevent users from submitting a form by hitting Enter

后端 未结 30 3427
感动是毒
感动是毒 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:52

    Go into your css and add that to it then will automatically block the submission of your formular as long as you have submit input if you no longer want it you can delete it or type activate and deactivate instead

     input:disabled {
            background: gainsboro;
          }
          input[value]:disabled {
            color: whitesmoke;
          }
    
    0 讨论(0)
  • 2020-11-21 05:53

    I had to catch all three events related to pressing keys in order to prevent the form from being submitted:

        var preventSubmit = function(event) {
            if(event.keyCode == 13) {
                console.log("caught ya!");
                event.preventDefault();
                //event.stopPropagation();
                return false;
            }
        }
        $("#search").keypress(preventSubmit);
        $("#search").keydown(preventSubmit);
        $("#search").keyup(preventSubmit);
    

    You can combine all the above into a nice compact version:

        $('#search').bind('keypress keydown keyup', function(e){
           if(e.keyCode == 13) { e.preventDefault(); }
        });
    
    0 讨论(0)
  • 2020-11-21 05:53

    Use:

    $(document).on('keyup keypress', 'form input[type="text"]', function(e) {
      if(e.keyCode == 13) {
        e.preventDefault();
        return false;
      }
    });
    

    This solution works on all forms on a website (also on forms inserted with Ajax), preventing only Enters in input texts. Place it in a document ready function, and forget this problem for a life.

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

    There are many good answers here already, I just want to contribute something from a UX perspective. Keyboard controls in forms are very important.

    The question is how to disable from submission on keypress Enter. Not how to ignore Enter in an entire application. So consider attaching the handler to a form element, not the window.

    Disabling Enter for form submission should still allow the following:

    1. Form submission via Enter when submit button is focused.
    2. Form submission when all fields are populated.
    3. Interaction with non-submit buttons via Enter.

    This is just boilerplate but it follows all three conditions.

    $('form').on('keypress', function(e) {
      // Register keypress on buttons.
      $attr = $(e.target).attr('type);
      if ($attr === 'button' || $attr === 'submit') {
        return true;
      }
    
      // Ignore keypress if all fields are not populated.
      if (e.which === 13 && !fieldsArePopulated(this)) {
        return false;
      }
    });

    0 讨论(0)
  • 2020-11-21 05:54
    $(document).on("keydown","form", function(event)
    {
       node = event.target.nodeName.toLowerCase();
       type = $(event.target).prop('type').toLowerCase();
    
       if(node!='textarea' && type!='submit' && (event.keyCode == 13 || event.keyCode == 169))
       {
            event.preventDefault();
            return false;
        }
    });
    

    It works perfectly!

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

    I needed to prevent only specific inputs from submitting, so I used a class selector, to let this be a "global" feature wherever I need it.

    <input id="txtEmail" name="txtEmail" class="idNoEnter" .... />
    

    And this jQuery code:

    $('.idNoEnter').keydown(function (e) {
      if (e.keyCode == 13) {
        e.preventDefault();
      }
    });
    

    Alternatively, if keydown is insufficient:

    $('.idNoEnter').on('keypress keydown keyup', function (e) {
       if (e.keyCode == 13) {
         e.preventDefault();
       }
    });
    

    Some notes:

    Modifying various good answers here, the Enter key seems to work for keydown on all the browsers. For the alternative, I updated bind() to the on() method.

    I'm a big fan of class selectors, weighing all the pros and cons and performance discussions. My naming convention is 'idSomething' to indicate jQuery is using it as an id, to separate it from CSS styling.

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