Home > Article > Web Front-end > JavaScript method to prevent carriage return from submitting form_javascript tips
Everyone should be familiar with the function of the Enter key. For example, in many applications in the Windows system, just click the Enter key to enter the program or enable a certain function. However, sometimes we want to prevent its function. For example, when filling out a form, you may accidentally click the Enter key and cause the form to be submitted by mistake. Here is a brief introduction on how to implement this function. The code example is as follows:
How to prevent the enter key from submitting a form is actually very simple, just one sentence. onkeydown="if(event.keyCode==13)return false;" Just write this sentence in the from tag.
<!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <title>屏蔽回车提交表单功能</title> <script type="text/javascript"> window.onload=function() { var myform=document.getElementById("myform"); myform.onkeypress=function(ev) { var ev=window.event||ev; if(ev.keyCode==13||ev.which==13) { return false; } } } </script> </head> <body> <p>在表单中回车默认会提交表单,在form的onkeypress事件中处理,只要返回false就可禁用回车提交表单</p> <form id="myform"> <input type="text" name="username"/> <input type="submit" value="提交"/> </form> </body> </html>
The above code can block the function of clicking Enter to submit the form. The code is simple and easy to understand. If you don’t understand it, you are welcome to give us your valuable opinions. Thank you for your continued support of the Script House website.