首页 > 代码库 > keypress
keypress
onKeyUp="keypress2(this,140)" onblur="keypress2(this,140)"
<span>你还可以输入140个字</span>
onKeyUp="keypress1(this,30)"
<span>你还可以输入30个字</span>
<script type="text/javascript">
function keypress1(t,max) //text输入长度处理
{
var text1 = $(t).val();
var len = max - text1.length;
var show = "你还可以输入" + len + "个字";
$(t).next().html(show);
}
function keypress2(t,max) //textarea输入长度处理
{
var text1 = $(t).val();
var len; //记录剩余字符串的长度
if (text1.length >= max)//textarea控件不能用maxlength属性,就通过这样显示输入字符数了
{
$(t).val(text1.substr(0, max));
len = 0;
}
else {
len = max - text1.length;
}
var show = "你还可以输入" + len + "个字";
$(t).next().html(show);
}
</script>