Home  >  Article  >  Web Front-end  >  How to use JS to block the Enter key in the page to submit the form

How to use JS to block the Enter key in the page to submit the form

高洛峰
高洛峰Original
2016-12-05 13:07:531190browse

For example, when the JS code is set to respond to the Enter key of the e388a4556c0f65e1904146cc1a846bee tag to trigger an event, according to the bubbling event principle, the event will be transmitted to the 66fd2ada9ebb04d4250c850dc1e3737e form and the form will be submitted. This is not the effect we want. We can set the following code to block it:

$(document).keydown(function(event){
  switch(event.keyCode){
     case 13:return false;
     }
});

However, if there is a button on the page, the form will also be submitted in the Opera browser. This is because the button is in the generated HTML The code is of submit type. The solution is to set UseSubmitbehavior="false" in the button so that the button is of button type in the HTML code generated on the page.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn