当前位置: 代码迷 >> JavaScript >> javascript对输入框的篇幅限制(转自愚人码头)
  详细解决方案

javascript对输入框的篇幅限制(转自愚人码头)

热度:90   发布时间:2012-11-23 00:03:43.0
javascript对输入框的字数限制(转自愚人码头)

此代码转载自愚人码头的博客:!),我觉得不错!适合初学者看看。

?

?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var textCounter=function (field,counter,maxlimit){
var charcnt = field.value.length;
if (charcnt > maxlimit) {
field.value = field.value.substring(0, maxlimit);
}else {
document.getElementById(counter).innerHTML=charcnt;
}
}
</script>
</head>
<body>
<p><span id="progressbar1">0</span>/12</p>
<form><input type="text" id="maxcharfield" size="34"/>
</form>
<script type="text/javascript">
var obj=document.getElementById("maxcharfield");
obj.onfocus = obj.onkeydown = obj.onkeyup = function(){
textCounter(this,'progressbar1',12);
}
</script>
</body>
</html>

  相关解决方案