js實例教程-一個基於jquery的文本框記數器

发布时间:2018-11-23 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js實例教程-一個基於jquery的文本框記數器脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小寶典致力於為廣大程序猿(媛)提供高品質的代碼服務,請大家多多光顧小站,小寶典在此謝過。

代碼如下:

/*
*長度跟蹤器
*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);


一、上面是計數器的腳本,可將腳本貼到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>


二、上面的代碼是展示如何使用。
這個計數器是自己用jQuery寫的,因此要依賴於jQuery庫函數。能計算回車符,因為在textarea裡面的回車符提交到後台后是'/r/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 &amp;& _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);


一、上面是計數器的腳本,可將腳本貼到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>


二、上面的代碼是展示如何使用。
這個計數器是自己用jQuery寫的,因此要依賴於jQuery庫函數。能計算回車符,因為在textarea裡面的回車符提交到後台后是'/r/n'。

覺得可用,就經常來吧!Javascript技巧 腳本寶典 歡迎評論哦! js技巧,巧奪天工,精雕玉琢。小寶典獻醜了!

脚本宝典总结

以上是脚本宝典为你收集整理的js實例教程-一個基於jquery的文本框記數器全部内容,希望文章能够帮你解决js實例教程-一個基於jquery的文本框記數器所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。