Prevent users from submitting a form by hitting Enter

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

    If you use a script to do the actual submit, then you can add "return false" line to the onsubmit handler like this:

    <form onsubmit="return false;">
    

    Calling submit() on the form from JavaScript will not trigger the event.

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

    You could make a JavaScript method to check to see if the Enter key was hit, and if it is, to stop the submit.

    <script type="text/javascript">
      function noenter() {
      return !(window.event && window.event.keyCode == 13); }
    </script>
    

    Just call that on the submit method.

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

    You can also use javascript:void(0) to prevent form submission.

    <form action="javascript:void(0)" method="post">
        <label for="">Search</label>
        <input type="text">
        <button type="sybmit">Submit</button>
    </form>
    

    <form action="javascript:void(0)" method="post">
        <label for="">Search</label>
        <input type="text">
        <button type="sybmit">Submit</button>
    </form>

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

    Instead of preventing users from pressing Enter, which may seem unnatural, you can leave the form as is and add some extra client-side validation: When the survey is not finished the result is not sent to the server and the user gets a nice message telling what needs to be finished to complete the form. If you are using jQuery, try the Validation plugin:

    http://docs.jquery.com/Plugins/Validation

    This will require more work than catching the Enter button, but surely it will provide a richer user experience.

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

    I can't comment yet, so I'll post a new answer

    Accepted answer is ok-ish, but it wasn't stopping submit on numpad enter. At least in current version of Chrome. I had to alter the keycode condition to this, then it works.

    if(event.keyCode == 13 || event.keyCode == 169) {...}
    
    0 讨论(0)
  • 2020-11-21 05:38

    You can use a method such as

    $(document).ready(function() {
      $(window).keydown(function(event){
        if(event.keyCode == 13) {
          event.preventDefault();
          return false;
        }
      });
    });
    

    In reading the comments on the original post, to make it more usable and allow people to press Enter if they have completed all the fields:

    function validationFunction() {
      $('input').each(function() {
        ...
    
      }
      if(good) {
        return true;
      }
      return false;
    }
    
    $(document).ready(function() {
      $(window).keydown(function(event){
        if( (event.keyCode == 13) && (validationFunction() == false) ) {
          event.preventDefault();
          return false;
        }
      });
    });
    
    0 讨论(0)
提交回复
热议问题