<div class="codetitle"> <span><a style="CURSOR: pointer" data="90397" class="copybut" id="copybut90397" onclick="doCopy('code90397')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code90397"> <br> <head> <br> <title>JS限制Textarea文本域字符个数</title> <br> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <br> <SCRIPT LANGUAGE="JavaScript"> <br> <!--// <BR> function textCounter(field, countfield, maxlimit) { <BR> // 函数,3个参数,表单名字,表单域元素名,限制字符; <BR> if (field.value.length > maxlimit) <br> //如果元素区字符数大于最大字符数,按照最大字符数截断; <br> fieldfield.value = field.value.substring(0, maxlimit); <br> else <br> //在记数区文本框内显示剩余的字符数; <br> countfield.value = maxlimit - field.value.length; <br> } <br> --> <br> </SCRIPT> <br> </head> <br> <body> <br> <form name=myform action=""> <br> <textarea name="message" cols="28" rows="5" onKeyDown="textCounter(message,remLen,6);" onKeyUp=<BR>"textCounter(message,remLen,6);"></textarea> <br> 您还可以输入:<input name="remLen" type="text" value="6" size="5" readonly="readonly">个字符 <br> </form> <br> </body> <br> </html><br> </div>