Как я могу заблокировать дальнейший ввод в textarea, используя maxlength

У меня есть текстовая область, на которой я хочу заблокировать ввод, если введенные символы достигают максимальной длины.

В настоящее время у меня есть скрипт Jquery для текстового поля, который вычисляет введенные символы, и хочу добавить что-то, что будет блокировать ввод в текстовой области после ввода 150 символов.

Я пытался использовать плагины максимальной длины вместе с моим скриптом, но они, похоже, не работают. Помощь приветствуется.

ТЕКУЩИЙ КОД

(function($) {
    $.fn.charCount = function(options){
        // default configuration properties
        var defaults = {    
            allowed: 150,       
            warning: 25,
            css: 'counter',
            counterElement: 'span',
            cssWarning: 'warning',
            cssExceeded: 'exceeded',
            counterText: '',
            container: undefined // New option, accepts a selector string
        }; 

        var options = $.extend(defaults, options); 

        function calculate(obj,$cont) {
              // $cont is the container, now passed in instead.
            var count = $(obj).val().length;
            var available = options.allowed - count;
            if(available <= options.warning && available >= 0){
                $cont.addClass(options.cssWarning);
            } else {
                $cont.removeClass(options.cssWarning);
            }
            if(available < 0){
                $cont.addClass(options.cssExceeded);
            } else {
                $cont.removeClass(options.cssExceeded);
            }
            $cont.html(options.counterText + available);
        };

        this.each(function() {
         // $container is the passed selector, or create the default container
            var $container = (options.container)
                    ? $(options.container)
                        .text(options.counterText)
                        .addClass(options.css)
                    : $('<'+ options.counterElement +' class="' + options.css + '">'+ options.counterText +'</'+ options.counterElement +'>').insertAfter(this);
            calculate(this,$container);
            $(this).keyup(function(){calculate(this,$container)});
            $(this).change(function(){calculate(this,$container)});
        });

    };
})(jQuery);
5
задан user342391 26 August 2010 в 19:19
поделиться