<form action="liuyan.php" method="post" name="frm" onsubmit="check();">
输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onkeyup="checkLength(this);" ></textarea>
<div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
</form>这是html页面的代码
下面是js的代码<script>
function checkLength(which){
var maxChars = 250;
if (which.value.length > maxChars)
which.value = which.value.substring(0,maxChars);
var curr = maxChars - which.value.length;
document.getElementById("chLeft").innerHTML = curr.toString();
}
function check() {
if (document.frm.liuyan.value=="") {
alert("留言内容不能为空!");
return false;
}
}
</script>可以动态的计算文本框的输入情况,但是汉字这里还是按照一个字符计算的,请教高手,如果输入汉字的话怎么算2个字符

解决方案 »

  1.   

    一般不会这样计算,那个是二进制的计算方法,在输入框中一般都是value.length来计算了,如果你想的话,就把其转化成二进制或者length*2
      

  2.   


        for(var   i=0;i<str.length;i++) {
            var temp = str.charCodeAt(i);
            if(temp >= 0 && temp <= 254){     }判断字符串的ASCII值是否在中文字符的区间吧,在的话算2个,不在算1个
      

  3.   

    来个高效率的^_^
    function checkLength(which){
        var maxChars = 250;
        var re = /[ox0-oxff]/gi;
        var oLength = which.value.replace(re,"").length;
        if (which.value.length+oLength > maxChars){
            which.value = which.value.substring(0,maxChars-oLength);
        }
        var curr = maxChars - which.value.length-oLength;
        document.getElementById("chLeft").innerHTML = curr.toString();
    }
    另外最好把onkeyup改成onpropertychange
      

  4.   

    不好意思,楼上的,由于本人还是菜鸟,onpropertychange="checkLength(this);"不能用啊,这两个用法难道不是一样的么?
      

  5.   

    <<script>
    function checkLength(which){
        var maxChars = 250;
        if (textbyte(which.value) > maxChars)
            which.value = gettextBybyte(which.value,maxChars);
        var curr = maxChars - textbyte(which.value);
        document.getElementById("chLeft").innerHTML = curr.toString();
    }
    function check() {
        if (document.frm.liuyan.value=="") {
            alert("留言内容不能为空!");
            return false;
        }
    }
    function textbyte(str){
        var count=0;
        for(i=0;i<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
        return count;
    }
    function gettextBybyte(str,len){
        var count=0;
        var rtn=""
        for(i=0;count<len;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
        return str.substring(0,count>50?i-1:i);
    }</script><form action="liuyan.php" method="post" name="frm" onsubmit="check();">
    输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onkeyup="checkLength(this);" ></textarea>
    <div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
    </form>
      

  6.   

    function checkLength(which){
        var maxChars = 250;
        var re = /[ox0-oxff]/gi;//匹配单字节字符
        var oLength = which.value.replace(re,"").length;//删除单字节字符,留下的全是双字节字符,实际字节长度 = 字符串长度 + 双字节个数
        if (which.value.length+oLength > maxChars){
            which.value = which.value.substring(0,maxChars-oLength);
        }
        var curr = maxChars - which.value.length-oLength;
        document.getElementById("chLeft").innerHTML = curr.toString();
    }
      

  7.   

    我想问的是,onpropertychange="checkLength(this);"
    这个不能用啊,语法规则是什么?可不可以透露下
      

  8.   


    补充一下:
    for(var i=0;i<str.length;i++) {
        var temp = str.charCodeAt(i);
        if(temp >= 0 && temp <= 254){
         alert("是英文字符");
      }else{
       alert("是中文字符");
      }
     }遍历字符串就行了,
    貌似字符长了效率不行的
      

  9.   

    onkeyup只考虑了用键盘操作的情况,如果右键粘贴的话则捕获不到了
    用onpropertychange则可以捕获值改变的任何情况,但是onpropertychange不被FF所支持
    在FF中需要使用oninput
      

  10.   

    大哥们,我想问的是语法,语法。小妹我都不知道怎么用啊,onpropertychange="checkLength(this);"难道这样写可以用吗?我为什么在谷歌浏览器里不能用啊
      

  11.   

    //IE only
    onpropertychange="checkLength(this);
      

  12.   

    这位仁兄的代码也有同样的一个问题,就是onkeyup,如果我是右键复制粘贴的话,剩余字符数没变,而任意按下一个键后才会发生变化。难道只能这样了吗?
      

  13.   

    <script>
    function checkLength(which,p){
        var maxChars = 250;
        
        if(p)which.value=which.value + ""+clipboardData.getData("text");
        if (textbyte(which.value) > maxChars)
            which.value = gettextBybyte(which.value,maxChars);
        var curr = maxChars - textbyte(which.value);
        document.getElementById("chLeft").innerHTML = curr.toString();
        return false;
    }
    function check() {
        if (document.frm.liuyan.value=="") {
            alert("留言内容不能为空!");
            return false;
        }
    }
    function textbyte(str){
        var count=0;
        for(i=0;i<str.length;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
        return count;
    }
    function gettextBybyte(str,len){
        var count=0;
        var rtn=""
        for(i=0;count<len;i++)(escape(str.charAt(i)).length< 4)?count++:count+=2;
        return str.substring(0,count>50?i-1:i);
    }</script><form action="liuyan.php" method="post" name="frm" onsubmit="check();">
    输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan" onpaste="return checkLength(this,'paste');" onkeyup="checkLength(this);"></textarea>
    <div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
    </form>
      

  14.   

    up up各位能不能支个招,可以在各个浏览器都能用的
      

  15.   

    我12楼不是回答了么
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>test</title>
    <script>
    window.onload = function(){
    if(navigator.userAgent.indexOf("MSIE")>=0) {    
     document.getElementById("liuyan").attachEvent('onpropertychange',function(o){   
      checkLength(document.getElementById("liuyan"));
     });   
    }else{     
     document.getElementById("liuyan").addEventListener('input',function(o){
     checkLength(document.getElementById("liuyan"));
     },false);   
    }
    };function checkLength(which){
        var maxChars = 250;
        var re = /[ox0-oxff]/gi;
        var oLength = which.value.replace(re,"").length;
        if (which.value.length+oLength > maxChars){
            which.value = which.value.substring(0,maxChars-oLength);
        }
        var curr = maxChars - which.value.length-oLength;
        document.getElementById("chLeft").innerHTML = curr.toString();
    }
    </script>
    </head><body>
    <form action="liuyan.php" method="post" name="frm" onsubmit="check();">
    输入您的留言:<br/><textarea name="liuyan" cols="50" rows="8" warp="soft" id="liuyan"></textarea>
    <div class="right"><font size="2">文字最大长度: 250. 剩余字符: <span id="chLeft">250</span>.</font></div>
    </form>
    </body></html>