作业帮 > 综合 > 作业

求高手指导 把这个javascript统计字数改成汉字统计为2个字符 英文数字统计为1个字符

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/07/05 14:35:14
求高手指导 把这个javascript统计字数改成汉字统计为2个字符 英文数字统计为1个字符

alert("不能超过"+total.value+"个字!");
}
else {
used.value = message.value.length;
remain.value = max - used.value;
}
}
-->

最多字数:

已用字数:

剩余字数:

xgc3098 是一种方法,我这里有另外一种方法,使用时,都是一样.如:value.getBytes() 或 value.charLength();
String.prototype.charLength=function(){
var _str=this.replace(/[\1-\xff]/ig,''),_len=_str.length;
return this.length + _len;
}
【使用】
假设有这样子的一个文本框:

String.prototype.charLength=function(){
var _str=this.replace(/[\1-\xff]/ig,''),_len=_str.length;
return this.length + _len;
}
function getLength(){
var c=document.getElementById('content');
document.getElementById('count').innerHTML=c.value.charLength();
}