jquery implementation of Sina Weibo get text box can enter the word code _jquery

Source: Internet
Author: User
Limit.js Code
Copy Code code as follows:

txt: text box jquery object
Limit: Limit the number of words
Isbyte:true: Limit as number of bytes; False: View limit as number of characters
CB: Callback function, parameter is the number of words can be entered
function Initlimit (TXT,LIMIT,ISBYTE,CB) {
Txt.keyup (function () {
var str=txt.val ();
var Charlen;
var bytelen=0;
if (isbyte) {//original blog: Blog.csdn.net/bluceyoung
for (Var i=0;i<str.length;i++) {
if (Str.charcodeat (i) >255) {
bytelen+=2;
}else{
bytelen++;
}
}
Charlen = Math.floor ((limit-bytelen)/2);
}else{
Bytelen=str.length;
Charlen=limit-bytelen;
}
CB (Charlen);
});
}

page Code
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta content= "text/html; Charset=utf-8 "http-equiv=" Content-type "/>
<script src= "Http://code.jquery.com/jquery-1.8.2.min.js" type= "Text/javascript" >
</script>

<script type= "Text/javascript" src= "Limit.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Initlimit ($ ("#txt"), 10,true,function (c) {
if (c>=0) {
$ ("#show"). Val ("Can also enter" the word "+c+");
}else{
$ ("#show"). Val ("Already Over" + (c) + "word");
}
});

Initlimit ($ ("#txt1"), 10,true,function (c) {
if (c>=0) {
$ ("#show1"). Val ("Can also enter" the word "+c+");
}else{
$ ("#show1"). Val ("Already Over" + (c) + "word");
}
});
});
</script>

<body>
<input type= "text" id= "TXT"/><input id= "show" type= "text"/><br/>
<input type= "text" id= "txt1"/><input id= "Show1" type= "text"/>

</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.