开发者

How to prevent input field inside form from submitting when enter key is pressed? [duplicate]

开发者 https://www.devze.com 2023-01-08 08:30 出处:网络
This question already has answers here: 开发者_JAVA百科 Prevent users from submitting a form by hitting Enter
This question already has answers here: 开发者_JAVA百科 Prevent users from submitting a form by hitting Enter (36 answers) Closed 6 years ago.

I have a simple form.

Input fields, checkboxes, radio buttons and finally SUBMIT button.

I use jQuery to perform AJAX validation, however when a user presses ENTER inside the input field it submits the form!

How do I stop this from happening on this form (not all input fields)?


suppose your inputs for which you don't want to submit on enter has class noSubmit then use this code:

$(function(){
     $("input.noSubmit").keypress(function(e){
         var k=e.keyCode || e.which;
         if(k==13){
             e.preventDefault();
         }
     });
 });


You can use the onsubmit handler to trap form submission and perform validation:

<form onsubmit="return validate();">
...
</form>

<script type="text/javascript">
function validate() {
  // return true to submit form or false to stop
}
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号