input 和 textarea 输入框最大文字限制的jquery插件 - Web前端
作者:98wpeu发布时间:2026-06-11分类:网页前端技术浏览:3
导读:复制代码代码如下:/*input和Textarea最大文字限定插件*修改版,一个中文表示1一个字,一个英文半个字;*textLimit-JQueryplu...
复制代码 代码如下:
/* input 和 Textarea 最大文字限定插件
* 修改版, 一个中文表示1一个字, 一个英文半个字;
* textLimit - JQuery plugin for counting and limiting characters for input and textarea fields
*
* pass '-1' as speed if you don't want the char-deletion effect. (don't just put 0)
* Example: jQuery("Textarea").textlimit('span.counter',256)
*
* $Version: 2009.07.25 +r2
* Copyright (c) 2009 YAIr even-Or
* vsync.design@Gmail.com
*/
string.prototype.getBytes = function () {
var cArr = this.match(/[^\x00-\xff]/ig);
return this.length + (cArr == null ? 0 : cArr.length);
};
(function(jquery) {
jquery.fn.textlimit=function(counter_el, thelimit, speed) {
var charDelSpeed = speed || 15;
var toggleCharDel = speed != -1;
var toggleTrim = true;
var that = this[0];
var isCtrl = false;
updateCounter();
function upDateCounter(){
if(typeof that == "Object")
JQuery(counter_el).text(thelimit - Math.ceil(that.value.getBytes()/2));
};
this.keydown (function(e){
if(e.which == 17) isCtrl = true;
var ctrl_a = (e.which == 65 && isCtrl == true) ? true : false; // detect and allow CTRL + A selects all.
var ctrl_v = (e.which == 86 && isCtrl == true) ? true : false; // detect and allow CTRL + V paste.
// 8 is 'backspace' and 46 is 'delete'
if( this.value.length >= thelimit && e.which != '8' && e.which != '46' && ctrl_a == false && ctrl_v == false)
e.PReventDefault();
})
.keyup (function(e){
updateCounter();
if(e.which == 17)
isCtrl=false;
if( this.value.length >= thelimit && toggleTrim ){
if(toggleCharDel){
// first, trim the text a bit so the char trimming won't take forever
// Also check if there are more than 10 extra chars, then trim. just in case.
if ( (this.value.length - thelimit) > 10 )
that.value = that.value.substr(0,thelimit+100);
var init = SetInterval
(
function(){
if( that.value.length <= thelimit ){
init = clearinterval(init); updateCounter()
}
else{
// deleting extra chars (one by one)
that.value = that.value.subString(0,that.value.length-1); jQuery(counter_el).text(Math.ceil(that.value.getBytes()/2));
}
} ,charDelSpeed
);
}
else this.value = that.value.substr(0,thelimit);
}
});
};
})(jQuery);
/* input 和 Textarea 最大文字限定插件
* 修改版, 一个中文表示1一个字, 一个英文半个字;
* textLimit - JQuery plugin for counting and limiting characters for input and textarea fields
*
* pass '-1' as speed if you don't want the char-deletion effect. (don't just put 0)
* Example: jQuery("Textarea").textlimit('span.counter',256)
*
* $Version: 2009.07.25 +r2
* Copyright (c) 2009 YAIr even-Or
* vsync.design@Gmail.com
*/
string.prototype.getBytes = function () {
var cArr = this.match(/[^\x00-\xff]/ig);
return this.length + (cArr == null ? 0 : cArr.length);
};
(function(jquery) {
jquery.fn.textlimit=function(counter_el, thelimit, speed) {
var charDelSpeed = speed || 15;
var toggleCharDel = speed != -1;
var toggleTrim = true;
var that = this[0];
var isCtrl = false;
updateCounter();
function upDateCounter(){
if(typeof that == "Object")
JQuery(counter_el).text(thelimit - Math.ceil(that.value.getBytes()/2));
};
this.keydown (function(e){
if(e.which == 17) isCtrl = true;
var ctrl_a = (e.which == 65 && isCtrl == true) ? true : false; // detect and allow CTRL + A selects all.
var ctrl_v = (e.which == 86 && isCtrl == true) ? true : false; // detect and allow CTRL + V paste.
// 8 is 'backspace' and 46 is 'delete'
if( this.value.length >= thelimit && e.which != '8' && e.which != '46' && ctrl_a == false && ctrl_v == false)
e.PReventDefault();
})
.keyup (function(e){
updateCounter();
if(e.which == 17)
isCtrl=false;
if( this.value.length >= thelimit && toggleTrim ){
if(toggleCharDel){
// first, trim the text a bit so the char trimming won't take forever
// Also check if there are more than 10 extra chars, then trim. just in case.
if ( (this.value.length - thelimit) > 10 )
that.value = that.value.substr(0,thelimit+100);
var init = SetInterval
(
function(){
if( that.value.length <= thelimit ){
init = clearinterval(init); updateCounter()
}
else{
// deleting extra chars (one by one)
that.value = that.value.subString(0,that.value.length-1); jQuery(counter_el).text(Math.ceil(that.value.getBytes()/2));
}
} ,charDelSpeed
);
}
else this.value = that.value.substr(0,thelimit);
}
});
};
})(jQuery);
- 上一篇:初学Jquery插件制作 在SageCRM的查询屏幕隐藏部分行的功能 - Web前端
- 下一篇:已经是最后一篇了
相关推荐
- jQuery的deferred对象使用详解 - Web前端
- ASP.NET jQuery 实例9 通过控件hyperlink实现返回顶部效果 - Web前端
- 初学Jquery插件制作 在SageCRM的查询屏幕隐藏部分行的功能 - Web前端
- jquery中dom操作和事件的实例学习 下拉框应用 - Web前端
- JQuery里面的几种选择器 查找满足条件的元素$("#控件ID") - Web前端
- 自己动手制作jquery插件之自动添加删除行的实现 - Web前端
- 在IE浏览器中resize事件执行多次的解决方法 - Web前端
- html5的自定义data-*属性和jquery的data()方法的使用示例 - Web前端
- jQuery的deferred对象使用详解 - Web前端
- 强大的jquery插件jqeuryUI做网页对话框效果!简单 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


