简体   繁体   English

我的jquery插件中的“ TypeError:$(…).characterCounter不是函数”

[英]'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.

相关问题 Jquery 验证插件 - TypeError: $(...).validate 不是函数 - Jquery validation plugin - TypeError: $(…).validate is not a function jQuery插件-调用函数返回Uncaught TypeError - jQuery Plugin - Calling a function return Uncaught TypeError Uncaught TypeError:未定义不是自定义jQuery插件的函数 - Uncaught TypeError: undefined is not a function with custom jQuery Plugin TypeError:jQuery(…).plugin-name不是一个函数 - TypeError: jQuery(…).plugin-name is not a function jquery插件Uncaught TypeError:object不是函数 - jquery plugin Uncaught TypeError: object is not a function 无法让我的jQuery插件正常工作。 TypeError:$(…).functionName不是函数 - Having trouble getting my jQuery plugin to work. TypeError: $(…).functionName is not a function 未捕获的TypeError:未定义不是我的jQuery脚本中的函数 - Uncaught TypeError: undefined is not a function in my jQuery script jQuery treeTable插件抛出错误“ TypeError:$(…).treetable不是函数” - jQuery treeTable Plugin throwing error “TypeError: $(…).treetable is not a function” 如何在我的jquery插件中停止Interval函数 - how to stop an Interval function in my jquery plugin jquery webcam plugin TypeError:webcam.capture不是偶尔抛出的函数 - jquery webcam plugin TypeError: webcam.capture is not a function thrown occasionally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM