[JavaScript] JavaScript监听文本框回车事件并过滤文本框空格 →→→→→进入此内容的聊天室

来自 , 2021-02-27, 写在 JavaScript, 查看 135 次.
URL http://www.code666.cn/view/8617f303
  1.     <script type="text/javascript" language="javascript">
  2.        
  3.         var username = null;
  4.         var password = null;
  5.        
  6.         //获取文本框        
  7.         onload = function()
  8.         {
  9.             username = document.getElementById("txtUserName");
  10.             password = document.getElementById("txtPassWord");            
  11.         }
  12.        
  13.         //清空文本框
  14.         function clearTxt()
  15.         {
  16.             username.value = "";
  17.             password.value = "";
  18.             username.focus();
  19.            
  20.             //  document.getElementById('txtUserName').value="";
  21.             //  document.getElementById('txtPassWord').value="";
  22.             //  document.getElementById('txtUserName').focus();        
  23.         }
  24.        
  25.         //确定
  26.         function chkTxt()
  27.         {
  28.             //删除前后空格
  29.             username.value = username.value.replace(/(^\s*)|(\s*$)/g,"");
  30.             password.value = password.value.replace(/(^\s*)|(\s*$)/g,"");
  31.             //判空
  32.             if(username.value.length == 0)
  33.             {
  34.                 alert("请输入用户名!");
  35.                 username.focus();
  36.             }
  37.             else if(password.value.length == 0)
  38.             {
  39.                 alert("请输入密码!");
  40.                 password.focus();
  41.             }
  42.             else
  43.             document.getElementById("btnLogin").click();
  44.         }
  45.         //回车监听
  46.         function onkey()
  47.         {
  48.             if (window.event.keyCode==13)
  49.             {
  50. //                document.all["btnLogin"].focus();
  51.                
  52. //                if(document.activeElement.id = "aReset")//判断当前焦点所在的控件的id是aReset
  53. //                {
  54. //                    document.getElementById("aReset").focus();
  55. //                }
  56.                 document.getElementById("aLogin").focus();
  57.                 return false;
  58.             }
  59.         }
  60.     </script>
  61.  
  62.  
  63. //javascript/6417

回复 "JavaScript监听文本框回车事件并过滤文本框空格"

这儿你可以回复上面这条便签

captcha