[英]'TypeError: $(…).characterCounter is not a function' in my jquery plugin
I am practicing the jQuery plugin 我正在练习jQuery插件
i create this plugin : 我创建了这个插件:
(function ($) {
'use strict';
$.fn.characterCounter = function () {
return this.each(function (index, item) {
$(item).keyup(function updateCharCounter() {
var $me = $(this),
maxLength = parseInt($me.attr('maxlength'), 10),
charCount = $me.val().length,
$counter = $me.siblings('.limit');
$counter.text('limit: ' + maxLength + ' characters. remaining: ' + (maxLength - charCount));
});
});
};
})(jQuery);
and my html code : 和我的html代码:
<div class="form-group">
<textarea id="short-message" maxlength="100" cols="8"></textarea>
<p class="limit">limit: 100 characters</p>
</div>
<div class="form-group">
<textarea id="short-shory" maxlength="150" cols="8"></textarea>
<p class="limit">limit: 150 characters</p>
</div>
<div class="form-group">
<input id="nickname" maxlength="50" size="32"/>
<p class="limit">limit: 50 characters</p>
</div>
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/showCharacterCharlimit.js"></script>
<script>
$(document)
.ready(function() {
'use strict';
$('textarea').characterCounter();
});
</script>
but get me this error : 但是让我这个错误:
TypeError: $(...).characterCounter is not a function TypeError:$(...)。characterCounter不是函数
i don't know why, could anybody help me ? 我不知道为什么,有人可以帮助我吗?
(function ($) { 'use strict'; $.fn.characterCounter = function () { return this.each(function (index, item) { $(item).keyup(function updateCharCounter() { var $me = $(this), maxLength = parseInt($me.attr('maxlength'), 10), charCount = $me.val().length, $counter = $me.siblings('.limit'); $counter.text('limit: ' + maxLength + ' characters. remaining: ' + (maxLength - charCount)); }); }); }; })(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <textarea id="short-message" class="wordcount" maxlength="100" cols="8"></textarea> <p class="limit">limit: 100 characters</p> </div> <div class="form-group"> <textarea id="short-shory" class="wordcount" maxlength="150" cols="8"></textarea> <p class="limit">limit: 150 characters</p> </div> <div class="form-group"> <input id="nickname" class="wordcount" maxlength="50" size="32"/> <p class="limit">limit: 50 characters</p> </div> <script> $(document) .ready(function() { 'use strict'; $('.wordcount').characterCounter(); }); </script>
Please check , you have a jquery dependency So you need to add jquery for this. 请检查,您具有jquery依赖项,因此您需要为此添加jquery。 And I used a Common Class So that it will work for all type of input fields. 而且我使用了通用类 ,以便它适用于所有类型的输入字段。
Thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.