input框计数功能:计算你输入input框中的字符长度;
功能比较简单,代码如下:
plugin.js
/**
* Created by yangjiao on 2017/8/1.
*/
"use strict";
(function ($) {
$.fn.countNum = function () {
var ele = this;
ele.on('change',function () {
String.prototype.len = function(){
return this.replace(/[^\x00-\xff]/g, "xx").length;
};
alert('您输入的字符长度为:'+$(this).val().len())
});
}
})(jQuery);
示例html如下:
plugin.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="plugin.js"></script>
</head>
<body>
<input type="text" id="count">
<script>
$(function () {
$('#count').countNum();
})
</script>
</body>
</html>