javascript限制文本域(文本框)输入字数方法二

来源:岁月联盟 编辑:zhuzhu 时间:2009-10-17

用javascript对文本框或文本域输入字数的限制,一般可用于网站评论等。

代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript对文本框或文本域输入字数的限制-php自学网</title>
</head>
<body>
<p><span id="progressbar1">0</span>/12</p>
<form><input type="text" id="maxcharfield" maxlength="12"/>
</form>
<script type="text/javascript">
document.getElementById("maxcharfield").onfocus = document.getElementById("maxcharfield").onkeydown = document.getElementById("maxcharfield").onkeyup = function(){
document.getElementById("progressbar1").innerHTML = this.value.length;
}
</script>
</body>
</html>