This example summarizes the method of submitting form by implementing the Enter key by JavaScript. Share it for your reference. The specific analysis is as follows:
The first method: javascript implementation
Copy the code as follows: <html>
<head>
<title> Javascript implements Enter key to submit form</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript">
document.onkeydown = function(ev){
ev = ev || window.event;
if(ev.keyCode == 13){
alert("I'm going to submit the form");
}
}
</script>
</head>
<body>
<h3> Press Enter to see a surprise</h3>
</body>
</html>
The second method: jquery implementation (ei8, chrome and firefox are supported)
Copy the code as follows: <html>
<head>
<title> Javascript implements Enter key to submit form</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function(){
$(document).keydown(function(e){
var curKey = e.which;
if(curKey == 13){
alert("I'm going to submit the form");
}
});
});
</script>
</head>
<body>
<h3> Press Enter to see a surprise</h3>
</body>
</html>
I hope this article will be helpful to everyone's web programming based on js.