js控制文本域textarea 剩余可填字符显示
Html部分:
<span class="remo">
<div>还可以输入
<span style="font-family: Georgia; font-size: 14px;" id="wordCheck">500</span>个字符</div>
<textarea onkeyup="javascript:checkWord(this);" onkeydown="javascript:checkWord(this);" rows="3" class="span12 enlemn" name="model3.reason" id="textarea1"></textarea>
</span>
Js方法:
var maxstrlen = 500;
function Q(s){
return document.getElementById(s);
}
function checkWord(c){
alert(c);
len = maxstrlen;
var str = c.value;
myLen = getStrleng(str);
var wck = Q("wordCheck");
if (myLen > len * 2) {
c.value = str.substring(0, i + 1);
}
else {
wck.innerHTML = Math.floor((len * 2 - myLen) / 2);
}
}
function getStrleng(str){
myLen = 0;
i = 0;
for (; (i < str.length) && (myLen <= maxstrlen * 2); i ++) {
if (str.charCodeAt(i) > 0 && str.charCodeAt(i) < 128)
myLen ++;
else
myLen += 2;
}
return myLen;
}
本文原创,转载必追究版权。