zoukankan      html  css  js  c++  java
  • 输入框字符限制插件·

    (function($) {
    
        $.fn.maxlength = function(settings) {
    
            if (typeof settings == 'string') {
                settings = { feedback: settings };
            }
    
            settings = $.extend({}, $.fn.maxlength.defaults, settings);
    
            function length(el) {
                var parts = $(el).attr("value");
                if (settings.words)
                    parts = $(el).attr("value").length ? parts.split(/\s+/) : { length: 0 };
                return parts.length;
            }
    
            return this.each(function() {
                var field = this,
                            $field = $(field),
                            limit = $field.attr('maxlength') || settings.limit,
                              $charsLeft = $("" + settings.feedback + "");
    
                function limitCheck(event) {
                    var len = length(this),
                                exceeded = len >= limit,
                                code = event.keyCode;
                    if (!exceeded)
                        return;
                    switch (code) {
                        case 8:  // allow delete
                        case 9:
                        case 17:
                        case 36: // and cursor keys
                        case 35:
                        case 37:
                        case 38:
                        case 39:
                        case 40:
                        case 46:
                        case 65:
                        case 67:
                            return;
                        default:
                            return settings.words && code != 32 && code != 13 && len == limit;
                    }
                }
    
                var updateCount = function() {
                    var len = length(field),
                            msg = "";
                    diff = limit - len;
                    if (len >= settings.middle) {
                        msg = settings.Msg + settings.DefaultMsg;
                    }
                    else {
                        msg = settings.DefaultMsg;
                    }
                    $charsLeft.html(msg.replace(/@/g, (diff || "0")));
                    // truncation code
                    if (settings.hardLimit && diff < 0) {
                        field.value = settings.words ?
                        // split by white space, capturing it in the result, then glue them back
                                field.value.split(/(\s+)/, (limit * 2) - 1).join('') :
                                field.value.substr(0, limit);
                        updateCount();
                    }
                };
    
                $field.bind('focus keyup input paste', function() {
                    setTimeout(function() {
                        updateCount();
                    }, 100);
                });
                if (settings.hardLimit) {
                    $field.keydown(limitCheck);
                }
                updateCount();
            });
        };
        $.fn.maxlength.defaults = {
            useInput: false,
            hardLimit: true,
            feedback: '#msg',
            words: false,
            middle: 70,
            limit: 140,
            Msg: "已经超过<span style='color:red'>70</span>个字,将分两条短信发送。",
            DefaultMsg: "还可输入<span style='color:green'>@</span>个字"
        };
    
    })(jQuery);

    对鼠标的粘贴事件也做了监视

  • 相关阅读:
    [js开源组件开发]ajax分页组件
    使用nodejs+express+socketio+mysql搭建聊天室
    [js开源组件开发]图片放大镜
    JY游戏之毁经典《扫雷》
    JY游戏之手游《打卡乐猫》
    [js开源组件开发]图片懒加载lazyload
    [js开源组件开发]js文本框计数组件
    [js开源组件开发]js轮播图片支持手机滑动切换
    [js开源组件开发]js手机联动选择日期 开源git
    [js开源组件开发]-手机端照片预览组件
  • 原文地址:https://www.cnblogs.com/youngerliu/p/2600859.html
Copyright © 2011-2022 走看看