弹出键值说明:
//console.log(ev.keyCode)
//回车:13
//ctrl:17
![](https://i-blog.csdnimg.cn/blog_migrate/5f94c2fecb4ccb2f76f50c965d122407.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/50828477840f9be8f01bb82af691c7d1.gif)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>留言提交系统</title> 6 <meta name="author" content="郭菊锋/702004176@qq.com"/> 7 </head> 8 9 <body> 10 <input type="text" name="" id="txt" value="" /> 11 <input type="button" name="" id="btn" value="提交" /><br /> 12 <textarea name="" rows="20" cols="80" id="txtArea"></textarea><br /> 13 </body> 14 </html>
![](https://i-blog.csdnimg.cn/blog_migrate/5f94c2fecb4ccb2f76f50c965d122407.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/50828477840f9be8f01bb82af691c7d1.gif)
1 <script type="text/javascript"> 2 window.onload = function() { 3 var oTxt = document.getElementById("txt"); 4 var oBtn = document.getElementById("btn"); 5 var oTxtArea = document.getElementById("txtArea"); 6 oBtn.onclick = function() { 7 addTxt(); 8 }; 9 oTxt.onkeydown = function(ev) { 10 var ev = ev || window.event; 11 //console.log(ev.keyCode) 12 if(ev.keyCode == 13 && ev.keyCode == 17) { 13 addTxt(); 14 } 15 if(ev.keyCode == 13) { 16 addTxt(); 17 } 18 }; 19 20 function addTxt() { 21 if(oTxt.value == "") { 22 alert("请输入内容"); 23 } 24 oTxtArea.value = oTxt.value; 25 oTxt.value = ""; 26 }; 27 } 28 </script>