如何限制文本框输入的字数
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language=JavaScript>
//取得字符串的字节长度
function ByteLen(Str){
var Len=0;
Str=Str + "";
for(var i=1;i<=Str.length;i++){
if(Str.charCodeAt(i-1)>255) Len+=2;
else Len+=1;
}
return(Len);
}
function ByteLeft(Str,Length){
var Len=0;
for(var i=1;i<=Str.length;i++)
{
if(Str.charCodeAt(i-1)>255) Len+=2;
else Len+=1;
if(Len==Length) return(Str.substring(0,i));
else if(Len>Length) return(Str.substring(0,i-1));
}
}
function Test(obj,Num){
var str=obj.value;
event.keyCode=0;
var len=ByteLen(obj.value);
document.all("status").innerText="剩余字节:" + (160-len);
if(ByteLen(str)>Num) obj.value=ByteLeft(str,Num);
}
</script>
<span id=status>剩余字节:160</span><br>
<textarea rows=5 cols=60 id=edit onPropertyChange="JavaScript:(Test(this,160))"></textarea>
[此贴子已经被作者于2004-05-18 12:25:56编辑过]