javascript显示剩余字数

textbox:
<textarea id="feedback" class="int" onkeyup="displaySpareNumber(this,300)" onchange="displaySpareNumber(this,300)"  name="content">

显示剩余字数:
 <id="spareNumberBox">还剩<span><input value="300" id="spareNumber" style="border:none; color:red; width:40px; text-align:center; background:none;" readonly="readonly"  /></span> 汉字</p>

javascript用于计算显示剩余字数(两个英文字母或数字算一个汉字):
function displaySpareNumber(_this,size)
{
    
var spareNumber=document.getElementById("spareNumber");    
    
var len=_this.value.replace(/[^\x00-\xff]/gi,'xx').length/2;
    var snum=Math.floor(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;            
    }        
}
posted @ 2009-09-28 16:45  ForFreeDom  阅读(565)  评论(0编辑  收藏  举报