黑莓5.1浏览器输入最大长度1不支持

时间:2013-11-25 07:20:28

标签: javascript html html5-canvas

function countChar(val) 
    {
    var xy=document.getElementsByID(val).value;
    var len = xy.length; 
    if (len >= 2) {   
       val.value = val.value.substring(0, 1); 
    } else {
        document.getElementsByID(val).value=(1 - len);  
    }
}

<input type="text" name="mytextbox" id="mytextbox" 
      onKeyUp="countChar(this.value)" size="1" maxlength="1" value="" />

1 个答案:

答案 0 :(得分:0)

将您的HTML设为:

<input type="text" name="mytextbox" id="mytextbox" 
    onKeyUp="countChar(this)" size="1" maxlength="1" value="" />

JS就是这样:

function countChar(that) 
{
   var xy = that.value;
   var len = xy.length;
   if (len >= 2) {
     that.value = that.value.substring(0, 1);
   } else {
     that.value=(1 - len);
   }
}