Home  >  Article  >  Web Front-end  >  Jquery implements imitation of Sina Weibo to obtain the code of the number of words that can be entered in the text box_jquery

Jquery implements imitation of Sina Weibo to obtain the code of the number of words that can be entered in the text box_jquery

WBOY
WBOYOriginal
2016-05-16 17:41:301129browse
limit.js code
Copy code The code is as follows:

// txt: text box jquery object
//limit: limited number of words
// isbyte: true: treat limit as number of bytes; false: treat limit as number of characters
// cb: callback function, parameters is the number of words that can be input
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;iif (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 The code is as follows:









< ;script type="text/javascript">
$(document).ready(function(){
InitLimit($("#txt"),10,true,function(c){
if(c>=0){
$("#show").val("You can also enter " c "characters");
}else{
$("#show"). val("has exceeded" (-c) "characters");
}
});

InitLimit($("#txt1"),10,true,function(c) {
if(c>=0){
$("#show1").val("You can also enter " c " characters");
}else{
$("# show1").val("Exceeded" (-c) "characters");
}
});
});











Jquery implements imitation of Sina Weibo to obtain the code of the number of words that can be entered in the text box_jquery
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn