日期:2014-05-16  浏览次数:20407 次

JS检查剩余输入字符的一个函数,求大家帮忙看看哪有错
大家   帮忙看看检测剩余字符的这个代码出错出在哪
<html>
<head>
<title> 剩余字符
</title>
<script   language= "javascript "   type= "text/javascript ">
function   scount(content,total,used,remain)
{
var   amax;
amax=total.value;
if   (content.value.length> amax)
{
content.value=content.value.substring(0,amax);
used.value=amax;
remain.value=0;
alert( "对不起你已经写满: "   +amax);

}
else
{
used.value=content.value.length;
remain.value=amax   -   used.value;
}
}

</script>
</head>
<body   onkeyi[>
<form   action= "5-14-2.html "   method= "post "   name= "reply "   id= "reply ">
<table>
<tr>
<td>
????:??:
<input   type= "text "   name= "total "   value= "1024 "   size= "5 "   id= "total ">
??:
<input   type= "text "   name= "used "   value= "0 "   size= "5 "   id= "use ">
??:
<input   type= "text "   name= "remain "   value= "1024 "   size= "5 "   id= "remain ">  
</td>
</tr>
<tr>
<td   align= "center "   >
<textarea   cols= "40 "   rows= "8 "   name= "content "   id= "content "       onKeyUp= "scount(this.form.content,this.form.total,this.form.used,this.form.remain) ">

</textarea>
</td>
</tr>
<tr>
<td   align= "center ">
<input   type= "submit "   name= "Submit "   value= "ok "   size= "10 ">
<input   type= "reset "   name= "Reset "   value= "?? "   size= "10 ">
</td>
</tr>
</table>
</form>
</body>
</html>
就是想实现文本框还可输入多少字符的一个函数,求大家看看!·谢谢了!


------解决方案--------------------
你那些得太繁琐 给你个简单的
<script Language= "JavaScript ">
maxLen = 200;
function checkMaxInput(form){
if (form.message.value.length > maxLen){
form.message.value = form.message.value.substring(0, maxLen);
} else {
form.remLen.value = maxLen - form.message.value.length;
}
}
<textarea name=message wrap=physical cols=28 rows=8 onKeyDown= "checkMaxInput(this.form) "> </textarea>
<input type=text name=remLen size=2 maxlength=3 value= "200 " readonly>