Core code
The code is as follows |
Copy Code |
JQuery (document). Ready (function ($) { var limitnum = 240; function limiting (obj, limit) { Console.log (obj); var cnt = obj.parent (). Find (". Counter > Span"); var txt = $ (obj). val (); var len = txt.length; var current = Limit-len;
$ (CNT). HTML (current);
Add Class warning If the remaining words are less than 0 if (current <= 0) { $ (CNT). addclass ("warning"); }else{ Remove the class warning after deleting the excess text $ (CNT). Removeclass ("warning"); } }
if ($ ("#textarea"). Length > 0) { Add a counter after textarea, if not empty, display the number of words already $ ("#textarea"). Parent (). Append ("<p class=" Counter "><span>" + (240-$ ("#textarea"). Val (). length) + </ Span> characters</p> "); $ (' #textarea '). KeyUp (function () { Limiting ($ (this), 240); }); } Usually Chinese is twice as big as English. if ($ ("#textarea_cn"). Length > 0) { $ ("#textarea_cn"). Parent (). Append ("<p class=" Counter "><span>" + (limitnum/2-$ ("#textarea_cn"). Val (). Length) + "</span> characters</p>"); $ (' #textarea_cn '). KeyUp (function () { Limiting ($ (this), LIMITNUM/2); }); } }); |
Example 2
Limit word Word input (add Chinese recognition)
The code is as follows |
Copy Code |
<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, CU Rlen = maxlen, len = v.length; for (var i = 0; i < v.length i++) { if (v.charcodeat (i) < 0 | | v.charcodeat (i) > 255) { Curle n-= 1; } } if (Curlen >= len) { $ ("#" +checklen). HTML ("also enter <strong>" +math.floor ((Curlen-len)/2) + "&L T;/strong> a word "). css (' color ', '); $ ("#subBtn"). Removeattr ("Disabled"); } else { $ ("#" +checklen). HTML ("has exceeded <strong>" +math.ceil ((Len-curlen)/2) + "</strong> word"). CSS (' Color ', ' #FF0000 '); $ ("#subBtn"). attr ("Disabled", "disabled"); } } </script> <textarea tabindex= "onkeyup=" Strlencalc ($ (this), ' Checklen ', #); "value=" "class=" px "name=" subject "id=" Subject "></textarea> <span id=" Checklen "> can also enter <strong>40</strong> Kanji </span> <input type= "Submit" id= "SUBBTN" value= "Publish"/> |