jquery实现textarea发布框限制文字字数输入(添加中文识别)
<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="" class="px" name="subject" id="subject"></textarea> <span id="checklen">还可输入 <strong>40</strong> 个汉字</span> <input type="submit" id="subBtn" value="发布" />
相关推荐
zengni 2020-04-16
Vipwxs 2018-12-19
javaer 2008-09-02
jscjxysx 2019-07-01
冯二爷 2019-07-01
Donutsapps 2019-06-29
无情 2018-12-19
原為簡單 2017-09-28
web开发 2017-01-16
勤能补拙孰能生巧 2019-06-28
wndong 2015-12-14
shishaoe 2015-04-10
csstpeixun 2015-04-02
xiaocaibai 2014-12-19
realitycss 2014-05-05
DreamLee 2013-12-13
切慕溪水 2013-07-11
lkj0 2013-05-24