JS 计算文本域剩余字数
<hr>
<textarea cols="20" rows="4" onkeyup="displaySpareNumber(this,10)" onchange="displaySpareNumber(this,10)" >
JS 计算文本域剩余字数
<hr>
<textarea cols="20" rows="4" onkeyup="displaySpareNumber(this,10)" onchange="displaySpareNumber(this,10)" ></textarea>
<script type="text/javascript" language="JavaScript">
function displaySpareNumber(_this,size)
{
var spareNumber=document.getElementById("spareNumber");
//汉字长度
var len=_this.value.replace(/[^\x00-\xff]/gi,'xx').length;
var snum=parseInt(size)-len;
spareNumber.value=snum;
if(snum<0)
{
if(_this.value.length!=len)
{
if((len-_this.value.length)>(size/2))
{
_this.value=_this.value.substring(0,size/2);
}
else
{
_this.value=_this.value.substring(0,size-(len-_this.value.length));
}
}
else
{
_this.value=_this.value.substring(0,size);
}
spareNumber.value=0;
return;
}
}
</script>
剩 <input id="spareNumber" name="spareNumber" value="10" size="1"/>
<br/><br/>
注: <br/>
displaySpareNumber(this,10) <br/>
与 <br/>
<input id="spareNumber" name="spareNumber" value="10" size="1"/> <br/>
中的10是你定义的内容长度.