37 lines
877 B
JavaScript
37 lines
877 B
JavaScript
var chlen = 0;
|
|
$(function(){
|
|
$("textarea").keydown(function(){
|
|
$(this).attr("maxlength","100");
|
|
if(this.value.length>=$(this).attr("maxlength")){
|
|
return;
|
|
}
|
|
});
|
|
$("textarea").keyup(function(){
|
|
chlen = 0;
|
|
if(this.value.length>$(this).attr("maxlength")){
|
|
return;
|
|
}
|
|
$(this).next().next().html("字数限制(<span style='color:red;'>"+this.value.length+"</span>--100)");
|
|
strlen(this.value);
|
|
if(this.value.length<$(this).attr("maxlength")&&chlen != 0){
|
|
$(this).attr("maxlength",eval(100+chlen));
|
|
}
|
|
});
|
|
});
|
|
|
|
function strlen(str){
|
|
var len = 0;
|
|
for (var i=0; i<str.length; i++) {
|
|
var c = str.charCodeAt(i);
|
|
//字数加1
|
|
if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {
|
|
len++;
|
|
}
|
|
else {
|
|
//中文字数加2
|
|
len+=2;
|
|
chlen++;
|
|
}
|
|
}
|
|
return len;
|
|
} |