復制代碼 代碼如下:
<html>
<head>
<title>JS限制Textarea文本域字符個數</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<SCRIPT LANGUAGE="JavaScript">
<!--//
function textCounter(field, countfield, maxlimit) {
// 函數,3個參數,表單名字,表單域元素名,限制字符;
if (field.value.length > maxlimit)
//如果元素區字符數大於最大字符數,按照最大字符數截斷;
field.value = field.value.substring(0, maxlimit);
else
//在記數區文本框內顯示剩余的字符數;
countfield.value = maxlimit - field.value.length;
}
//-->
</SCRIPT>
</head>
<body>
<form name=myform action="">
<textarea name="message" cols="28" rows="5" onKeyDown="textCounter(message,remLen,6);" onKeyUp="textCounter(message,remLen,6);"></textarea>
您還可以輸入:<input name="remLen" type="text" value="6" size="5" readonly="readonly">個字符
</form>
</body>
</html>