<html>
<head>
<title> new document </title>
<script language="javascript">
</script>
</head>
<body onload="sel()">
<input type="text" name="t1" id="t1" onkeyup="document.getElementById('t2').value=this.value.length" maxlength="5">
<input type="text" name="t2" id="t2">(only 5)
</body>
</html>

解决方案 »

  1.   

    这里有答案
    http://blog.csdn.net/hztgcl1986/archive/2007/12/04/1915858.aspx
      

  2.   

    加上你所说的功能之后,
    捡了芝麻丢了西瓜,
    textarea会损失undo功能,让人难以接受.-----
    建议:
    textarea的长度校验放在textarea的onblur或form的onsubmit里校验,
    以下函数仅供参考:
    代码来自JKValidation
    http://download.csdn.net/source/276201 
    /**
    *checkMaxLength.
    */
    function checkMaxLength(textareaObj,maxLength)
    {
    if(maxLength == null) maxLength=textareaObj.getAttribute("maxLength");
    if(maxLength == null) maxLength=1024;
    var currentLength = textareaObj.value.length;
    if (currentLength > maxLength) {
    alert("The length of your input ("+currentLength+") is larger than maxLength ("+maxLength+") .");
    if(textareaObj.createTextRange){
    var textRange=textareaObj.createTextRange();
    var enterMatch=textareaObj.value.substr(0,maxLength).match(/\n/ig);
    var enterNum=0;
    if(enterMatch) enterNum=enterMatch.length
    textRange.moveStart('character',maxLength-enterNum);
    textRange.select();
    }
    else {
    textareaObj.focus();
    textareaObj.setSelectionRange(maxLength, currentLength); 
    }
    window.latestValidateObj=textareaObj;
    setTimeout("window.latestValidateObj.focus();",0);
    return false;
    }
    return true;
    }