JS-制作留言提交系统(支持ctrl+回车)
弹出键值说明:
//console.log(ev.keyCode)
//回车:13
//ctrl:17
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>
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>
越努力,越幸运;阿门。