Home > Web Front-end > JS Tutorial > JavaScript method to prevent carriage return from submitting form_javascript tips

JavaScript method to prevent carriage return from submitting form_javascript tips

WBOY
Release: 2016-05-16 15:22:45
Original
1355 people have browsed it

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> 
Copy after login

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.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template