填写完表单数据后,很多用户喜欢直接按回车键提交,省去鼠标找“提交”按钮再单击的时间
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta charset="utf-8" />
5 <title>回车提交</title>
6 </head>
7 <body>
8 <h2>回车提交</h2>
9 <input type="text" id="enterSubmit" value="回车提交" />
10
11 <script type="text/javascript">
12 window.onload=function(){
13 document.getElementById("enterSubmit").onkeyup=function(e){
14 e=e||window.event;//获取事件对象
15 var keycode=e.keyCode||e.which||e.charCode;//获取键码
16 if(keycode===13){
17 alert("回车提交成功");
18 }
19 }
20 }
21 </script>
22 </body>
23 </html>
e.keyCode||e.which||e.charCode,当响应的键码为13时就是表示按下了回车键
来源:https://www.cnblogs.com/HollyLearning/p/5442551.html