一個基于jquery的文本框記數(shù)器
更新時間:2012年09月19日 23:13:06 作者:
基于jquery的文本框記數(shù)器實現(xiàn)代碼,需要的朋友可以參考下
復制代碼 代碼如下:
/*
*長度跟蹤器
*v2.1.0
*bind2Id:用于顯示長度變化的元素的id
*max:最大長度
*msgWrap:提示信息(必須要有一個"-"占位符)
*eg:$('#input').lenTracer({bind2Id:'myTracer',max:150,msgWrap:'您還可以輸入-個字符'});
*author:liujg1015@gmail.com
*/
(function ($) {
var zw_validate = function (el, option) {
this.el = $(el);
this.bindEl = false;
this.setting = {
bind2Id: false,
max: 100,
msgWrap: '您還可以輸入-個字符'
};
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);
一、上面是計數(shù)器的腳本,可將腳本貼到js文件中,然后在html里面添加引用。
復制代碼 代碼如下:
<html>
<head>
<title>demo</title>
</head>
<body>
<table>
<tr>
<td>
標題
</td>
<td>
<input type="text" id="title" />
<span id="titlelen"></span>
</td>
</tr>
<tr>
<td>
評論
</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>
二、上面的代碼是展示如何使用。
這個計數(shù)器是自己用jQuery寫的,因此要依賴于jQuery庫函數(shù)。能計算回車符,因為在textarea里面的回車符提交到后臺后是'\r\n'。歡迎批評、指正。
相關(guān)文章
jquery查找父元素、子元素(個人經(jīng)驗總結(jié))
對使用js或者jquery查找父元素、子元素比較混淆的朋友可以參考下本文,因為是個人總結(jié),用起來會比較方便2014-04-04將form表單通過ajax實現(xiàn)無刷新提交的簡單實例
下面小編就為大家?guī)硪黄獙orm表單通過ajax實現(xiàn)無刷新提交的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10jQuery getJSON 處理json數(shù)據(jù)的代碼
Ashx處理程序:如果需要返回json格式的對象,需要把mime類型設置為:"application/json"。2010-07-07關(guān)于jQuery.ajax()的jsonp碰上post詳解
這篇文章主要介紹了關(guān)于jQuery.ajax()的jsonp碰上post的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07為jQuery.Treeview添加右鍵菜單的實現(xiàn)代碼
jquery.treeview 數(shù)據(jù)通過JSON數(shù)據(jù)加載,有需要的朋友,可以通jquery的AJAX方法獲取相關(guān)的JSON數(shù)據(jù)。2010-10-10