我是新手    希望专家帮帮我
就是在输入框里输入字的时候  同时在旁边显示输入的字数    而输入的可以是汉字 也可以是英文
我刚进公司    公司让我做这个  希望大家能帮帮忙  

解决方案 »

  1.   


    ///IE
    <html>
    <head>
    <title> test</title></head>
    <body >
    <textarea cols="15" rows="25" onpropertychange="document.getElementById('txt').value=this.value.length"></textarea>
    <input type="text" value="" id="txt">
    </body>
    </html>
      

  2.   

    谢谢 啊   
    不过我要的是 显示字的个数  不是字母的个数  hello 显示为1个数  而不是5个数
      

  3.   

    晕 那也太智能了?? 那如果是错的单词 hemmo 这算几个?
      

  4.   

    对阿  他都这么说了 那hello不是5个字?
      

  5.   

    <html>
    <head>
    <title> test</title>
    <script>
    var num=0;
    function check(){ if(event.keyCode==32){//除非保证单词都正确然后敲空格
    num++;
    document.getElementById('txt').value=num;
    }
    }
    </script>
    </head>
    <body >
    <textarea cols="15" rows="25" onkeydown="check(this.value)"></textarea>
    <input type="text" value="" id="txt">
    </body>
    </html>
      

  6.   

    帮助下新手:
    用正则弄个onblur事件,代码如下:
    <html>
    <head>
    <title> test</title>
    <script>
    var num=0;
    function check(){//    if(event.keyCode==32){//除非保证单词都正确然后敲空格
    //    num++;
    //    document.getElementById('txt').value=num;
    //    }
    }
    function check2(str)
    {
        var num2 = 0;
        var reg = /\b/g;
        while(reg.exec(str))
        num2++;
        document.getElementById('txt').value=num2/2;
        
    }</script>
    </head>
    <body >
    <textarea cols="15" rows="25" onkeydown="check(this.value)" onblur="check2(this.value)"></textarea>
    <input type="text" value="" id="txt">
    </body>
    </html>
      

  7.   

    这样可以得到时时效果
    :<html>
    <head>
    <title> test</title>
    <script>function check(str){
    var num=0;
        
        var reg = /\b/g;
        while(reg.exec(str))
        num++;
        document.getElementById('txt').value=num/2;
    //    if(event.keyCode==32){//除非保证单词都正确然后敲空格
    //    num++;
    //    document.getElementById('txt').value=num;
    //    }
    }
    function check2(str)
    {
    //    var num2 = 0;
    //    var reg = /\b/g;
    //    while(reg.exec(str))
    //    num2++;
    //    document.getElementById('txt').value=num2/2;
        
    }</script>
    </head>
    <body >
    <textarea cols="15" rows="25" onkeydown="check(this.value)" onblur="check2(this.value)"></textarea>
    <input type="text" value="" id="txt">
    </body>
    </html>