jQuery.fn.truncate = function(len)
{
this.val(this.val().substring(0,len));
return false;
}
jQuery.fn.maxLength = function(len)
{
var maxLengthKeyPress = new Array();
var maxLengthChange = new Array();
var appleKeyOn = false;
//the second argument should be true if len should be based on
//the maxlength attribute instead of the input
var useAttr = arguments.length>1 ? arguments[1] : false;
var handleKeyUp = function(e)
{
//if the apple key (macs) is being pressed, set the indicator
if(e.keyCode==224||e.keyCode==91)
appleKeyOn = false;
}
var handleKeyDown = function(e)
{
//if the apple key (macs) is being released, turn off the indicator
if(e.keyCode==224||e.keyCode==91)
appleKeyOn = true;
}
var handleKeyPress = function(e)
{
//if this keyCode does not increase the length of the textarea value,
//just let it go
if(appleKeyOn || (e.charCode==0&&e.keyCode!=13) || e.ctrlKey)
return;
//get the textarea element
var textarea = $(this);
//if the length should be based on the maxlength attribute instead of the
//input, use that
len = useAttr ? parseInt(textarea.attr('maxlength')) : len;
//get the value of the textarea
var val = textarea.val();
//get the length of the current text selection
var selected = Math.abs(textarea.attr('selectionStart') - textarea.attr('selectionEnd'));
selected = isNaN(selected) ? 0 : selected;
//if this is the maximum length
if(val.length==len && selected<1)
return false;
else if(val.length>len && selected<(val.length-len))
return textarea.truncate(len);
};
var handleChange = function(e)
{
//get the textarea element
var textarea = $(this);
//if the length should be based on the maxlength attribute instead of the
//input, use that
len = useAttr ? parseInt(textarea.attr('maxlength')) : len;
//truncate the textarea to its proper length
textarea.truncate(len);
};
//get the current keyup and change functions
var removeKeyPress = maxLengthKeyPress[this.selector];
var removeChange = maxLengthChange[this.selector];
//remove the keyup and change functions from any matched elements in case
//a maxlength was previously set and a new one is being set
this.die('keypress', removeKeyPress);
this.die('change', removeChange);
if(len==0 && !useAttr)
return;
//set the keyup and change functions for this element set and all future
//elements matching this selector
this.live('keypress', handleKeyPress);
this.live('change', handleChange);
this.live('keydown', handleKeyDown);
this.live('keyup', handleKeyUp);
//save the current keyup and change functions so that they can be
//remove later
maxLengthKeyPress[this.selector] = handleKeyPress;
maxLengthChange[this.selector] = handleChange;
//trigger a keypress event so that the limit will be enforced
this.keypress();
};
$(function()
{
//limit the length of all textareas with the maxlength attribute
//NOTE: setting maxlength on a textarea is NOT valid XHTML. this
//was added for coders who want to code loosely--not me!
$('textarea[maxlength]').maxLength(null, true);
});
调用方法很简单
$("textarea").maxLength(255);
<textarea>这里的输入字数将会限制在255个字符</textarea>
对中文输入不支持 貌似此插件只是判断了键盘的事件 而没有判断内容的改变
最新的jQuery
http://docs.jquery.com/Downloading_jQuery
另:貌似有时候会和某些handle事件冲突,但是还没找到原因,正在研究。
分享到:
相关推荐
js和jquery分别实现对textarea字数的限制,精彩
jquery新浪发布微博textarea文本框限制文字字数表单 jquery新浪发布微博textarea文本框限制文字字数表单
jquery实现的关键字高亮插件,简单易用,连textarea中的关键字都可以高亮哦
jQuery插件charCount模仿twitter和新浪微博的textarea字数统计.zip
jQuery textarea文本框输入文字字数限制提示代码,设置固定输入字符,实时计算显示已经输入多少字符。
NULL 博文链接:https://xiaojin21cen.iteye.com/blog/1560818
jquery----TextArea高度自适应
本文实例讲述了jquery实现textarea输入框限制字数的方法。分享给大家供大家参考。具体分析如下: 网上有使用属性 disabled 来实现,这个不好,想修改都没有修改。当然,这个也不是很完美。 代码如下:<html> &...
Textarea Fullscreen是一个jquery插件,可以将textarea设置为全屏模式 使用方法 引用jquery.js,jquery.textareafullscreen.js和textareafullscreen.css <script type="text/javascript"> $(function(){ $('#demo'...
jquery怎么实现限制textarea输入的字数呢?有需要的朋友可以参考一下我写的例子,当然如果有误,也希望大家能及时指出来,大家共同学习进步。 <!DOCTYPE html> <html lang="en"> <head> <meta ...
Textarea Fullscreen是一个jquery插件大小只有5K,可以将textarea设置为全屏模式,
angularjs实现textarea文本输入字数限制功能
jquery 插件之 floattextbox 修改版,增加字符输入提示。 输入框插件:实现点击textarea输入框弹出大的输入框
jQuery textarea文本框输入文字字数限制提示代码,设置固定输入字符,实时计算显示已经输入多少字符。
主要为大家详细介绍了jQuery编写textarea输入字数限制代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
这是一款可为textarea添加行号的jquery插件。该插件可以为普通的textarea文本框在左侧添加一个行号,使用非常简单。
JQuery实现textarea可以有字数录入提醒功能