<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$("textarea").keyup();
});
function strLenCalc(obj, checklen, maxlen) {
var v = obj.val(), charlen = 0, maxlen = !maxlen ? 200 : maxlen, curlen = maxlen, len = v.length;
for(var i = 0; i < v.length; i++) {
if(v.charCodeAt(i) < 0 || v.charCodeAt(i) > 255) {
curlen -= 1;
}
}
if(curlen >= len) {
$("#"+checklen).html("还可输入 <strong>"+Math.floor((curlen-len)/2)+"</strong> 个字").css('color', '');
$("#subBtn").removeAttr("disabled");
} else {
$("#"+checklen).html("已经超过 <strong>"+Math.ceil((len-curlen)/2)+"</strong> 个字").css('color', '#FF0000');
$("#subBtn").attr("disabled", "disabled");
}
}
</script>
<textarea tabindex="11" onkeyup="strLenCalc($(this), 'checklen', 80);" value=""></textarea>
<span>还可输入 <strong>40</strong> 个汉字</span>
<input type="submit" value="发布" />
基于jquery的textarea发布框限制文字字数输入(添加
内容版权声明:除非注明,否则皆为本站原创文章。