zoukankan      html  css  js  c++  java
  • textArea中的maxlength是无效的 解决办法

    --------------------------------------------------------------------------------------
     
    <s:textarea name="doc.intro" id="intro" rows="3" cssStyle=" 505px;" onkeyup="validateLength(this)" onblur="validateLength(this)"></s:textarea>
     
     //验证textare输入长度
     function validateLength(arg){
      var intro = arg.innerHTML.replace(/^s+|s+$/g,"");
      if(intro.length>512){
       arg.innerHTML=intro.substr(0,512);
      }
     }
    -------------------------------------------------------------------------------------
    o 如果只是單純地想限制 textarea 中的字數,不想寫太多的話,可用:
     
    <textarea onkeyup="this.value = this.value.slice(0, 80)"></textarea>
     
     
    <textarea onkeyup="this.value = this.value.substring(0, 80)"></textarea>
     
     
    o 在 textarea 的 onkeyup 中檢查字數之方法:
     
    .....
    <script language="javascript" type="text/javascript">
    function DjCheckMaxlength(oInObj)
    {
     var iMaxLen = parseInt(oInObj.getAttribute('maxlength'));
     var iCurLen = oInObj.value.length;
     
     if ( oInObj.getAttribute && iCurLen > iMaxLen )
     {
       oInObj.value = oInObj.value.substring(0, iMaxLen);
     }
    } //@ END OF DjCheckMaxlength()
    </script>
    .....
    <textarea maxlength="80" onkeyup="return DjCheckMaxlength(this);"></textarea>
    .....
     
     
    o 設定好自動去檢查所有的 textarea 之方法:
     
    .....
    <head>
    <script language="javascript" type="text/javascript">
    function DjTextArea_AddMaxlengthAttr()
    {
     var aX = document.getElementsByTagName_r('textarea');
     
     for ( var iI = 0; aX.length > iI; iI++ )
     {
       if ( aX[iI].getAttribute('maxlength') )
       {
         aX[iI].onkeyup = aX[iI].onchange = DjTextArea_CheckMaxlength;
       }
     }
    } //@ END OF DjTextArea_AddMaxlengthAttr()
     
    function DjTextArea_CheckMaxlength()
    {
     var iMaxLen = parseInt(this.getAttribute('maxlength'));
     var iCurLen = this.value.length;
     
     if ( this.getAttribute && iCurLen > iMaxLen )
     {
       this.value = this.value.substring(0, iMaxLen);
     }
    } //@ END OF DjTextArea_CheckMaxlength()
    </script>
    </head>
    .....
    <body onload="DjTextArea_AddMaxlengthAttr();">
     
     
     
    首先在textarea 上定义一个 maxlength 属性:
     
    <textarea rows="3" cols="40" maxlength="200" onkeydown="checklength(this);"></textarea>
     
    然后添加这个方法就行了!
     
    function checklength(obj) {
       var max = obj.maxlength;
       if(max == null || max == "" || max == undefined) {
         return;
       }
       if(obj.value.length > max) {
         alert("请不要超过最大长度:" + max);
        obj.value=obj.value.substring(0,(max-1));
         return;
       }
     }
    挺方便吧!!!
  • 相关阅读:
    python 文件读写操作(转抄)
    kubernetes之kubeadmin安装部署
    bash之字符串处理(核心重点)
    blocking and nonblocking
    文件格式转换
    解压.asar
    Cocos Creator Editor 编辑器扩展记录
    CocosCreator 警告:Please set node's active instead of rigidbody's enabled
    Unity 垂直翻转位图颜色数据
    CocosCreator 动态设置属性在Properties面板显示/隐藏
  • 原文地址:https://www.cnblogs.com/yinchengliang/p/6163516.html
Copyright © 2011-2022 走看看