一個(gè)基于jquery的文本框記數(shù)器
更新時(shí)間:2012年09月19日 23:13:06 作者:
基于jquery的文本框記數(shù)器實(shí)現(xiàn)代碼,需要的朋友可以參考下
復(fù)制代碼 代碼如下:
/*
*長(zhǎng)度跟蹤器
*v2.1.0
*bind2Id:用于顯示長(zhǎng)度變化的元素的id
*max:最大長(zhǎng)度
*msgWrap:提示信息(必須要有一個(gè)"-"占位符)
*eg:$('#input').lenTracer({bind2Id:'myTracer',max:150,msgWrap:'您還可以輸入-個(gè)字符'});
*author:liujg1015@gmail.com
*/
(function ($) {
var zw_validate = function (el, option) {
this.el = $(el);
this.bindEl = false;
this.setting = {
bind2Id: false,
max: 100,
msgWrap: '您還可以輸入-個(gè)字符'
};
if (option) {
$.extend(this.setting, option);
this.init();
}
};
zw_validate.prototype = {
init: function () {
var _this = this;
this.bindEl = $('#' + this.setting.bind2Id);
this.el.focus(function () { _this.start(); }).blur(function () { _this.dispose(); });
this.el.css({ paddingBottom: 20 });
this.initMsg();
},
initMsg: function () {
var wrap = this.setting.msgWrap.split('-');
this.bindEl.text(this.setting.max - this.count().total).before(wrap[0]).after(wrap[1]);
},
count: function () {
var _val = this.el.val();
var _len = _val.length;
var _rowCount = 0;
var _enterLen = 0;
var _partten = /\n+/g;
if (_len > 0 && _partten.test(_val)) {
_enterLen += 3;
while ((result = _partten.exec(_val)) != null) {
if ((result.index + 1 + _enterLen) > this.setting.max) {
break;
}
_enterLen += 3;
}
_rowCount = _val.match(_partten).length;
}
return { total: (_len + _rowCount * 3), enterLen: _enterLen };
},
start: function () {
var _this = this;
_this.timer = setInterval(function () {
var _val = _this.el.val();
var _rlt = _this.count();
if (_rlt.total > _this.setting.max) {
if (_rlt.enterLen > 0) {
_this.el.val(_val.substr(0, _this.setting.max - _rlt.enterLen));
}
else {
_this.el.val(_val.substr(0, _this.setting.max));
}
_this.bindEl.text(_this.setting.max - _this.count().total);
return;
}
_this.bindEl.text(_this.setting.max - _rlt.total);
}, 300);
},
dispose: function () {
clearInterval(this.timer);
},
restore: function () {
this.bindEl.text(this.setting.max - this.el.val().length);
}
};
$.fn.extend({
lenTracer: function (option) {
return new zw_validate(this, option);
}
});
})(jQuery);
一、上面是計(jì)數(shù)器的腳本,可將腳本貼到j(luò)s文件中,然后在html里面添加引用。
復(fù)制代碼 代碼如下:
<html>
<head>
<title>demo</title>
</head>
<body>
<table>
<tr>
<td>
標(biāo)題
</td>
<td>
<input type="text" id="title" />
<span id="titlelen"></span>
</td>
</tr>
<tr>
<td>
評(píng)論
</td>
<td>
<textarea cols="5" rows="5" id="comment"></textarea>
<span id="commentlen"></span>
</td>
</tr>
</table>
<script src="../scripts/jquery.js" type="text/javascript"></script>
<script src="../scripts/lentracer.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#title).lenTracer({ bind2Id: titlelen, max: 50});
$('#comment).lenTracer({ bind2Id: commentlen, max: 200, msgWrap: '剩余-字' });
});
</script>
</body>
</html>
二、上面的代碼是展示如何使用。
這個(gè)計(jì)數(shù)器是自己用jQuery寫(xiě)的,因此要依賴(lài)于jQuery庫(kù)函數(shù)。能計(jì)算回車(chē)符,因?yàn)樵趖extarea里面的回車(chē)符提交到后臺(tái)后是'\r\n'。歡迎批評(píng)、指正。
您可能感興趣的文章:
- jquery實(shí)現(xiàn)文本框textarea自適應(yīng)高度
- jquery 插件實(shí)現(xiàn)多行文本框[textarea]自動(dòng)高度
- jquery 多行文本框(textarea)高度變化
- js/jquery獲取文本框輸入焦點(diǎn)的方法
- 基于jQuery的計(jì)算文本框字?jǐn)?shù)的代碼
- JQuery 文本框使用小結(jié)
- jquery對(duì)單選框,多選框,文本框等常見(jiàn)操作小結(jié)
- Jquery實(shí)現(xiàn)仿新浪微博獲取文本框能輸入的字?jǐn)?shù)代碼
- jQuery動(dòng)態(tài)改變多行文本框高度的方法
相關(guān)文章
jquery ajax return沒(méi)有返回值的解決方法
jquery ajax return沒(méi)有返回值的解決方法,需要的朋友可以參考下。2011-10-10jquery查找父元素、子元素(個(gè)人經(jīng)驗(yàn)總結(jié))
對(duì)使用js或者jquery查找父元素、子元素比較混淆的朋友可以參考下本文,因?yàn)槭莻€(gè)人總結(jié),用起來(lái)會(huì)比較方便2014-04-04將form表單通過(guò)ajax實(shí)現(xiàn)無(wú)刷新提交的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇將form表單通過(guò)ajax實(shí)現(xiàn)無(wú)刷新提交的簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10Jquery方式獲取iframe頁(yè)面中的 Dom元素
這篇文章主要介紹了Jquery方式如何獲取iframe頁(yè)面中的Dom元素,需要的朋友可以參考下2014-05-05jQuery getJSON 處理json數(shù)據(jù)的代碼
Ashx處理程序:如果需要返回json格式的對(duì)象,需要把mime類(lèi)型設(shè)置為:"application/json"。2010-07-07jQuery 實(shí)現(xiàn)圖片的依次加載圖片功能
本文分為css和js代碼兩部分給大家詳細(xì)介紹了jQuery 實(shí)現(xiàn)圖片的依次加載圖片功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07關(guān)于jQuery.ajax()的jsonp碰上post詳解
這篇文章主要介紹了關(guān)于jQuery.ajax()的jsonp碰上post的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07為jQuery.Treeview添加右鍵菜單的實(shí)現(xiàn)代碼
jquery.treeview 數(shù)據(jù)通過(guò)JSON數(shù)據(jù)加載,有需要的朋友,可以通jquery的AJAX方法獲取相關(guān)的JSON數(shù)據(jù)。2010-10-10