js控制用户输入必须是数字,但连续按键没进行控制

来源:岁月联盟 编辑:exp 时间:2012-04-25

var setvalueflag = false;
   
    $('.onlynumber').live("keydown",function(event){
        setvalueflag = Solok.util.onlyNum(event) ;
    });

    $('.onlynumber').live("keyup",function(event){
        if(!setvalueflag){
            var str = $(this).val();
            $(this).val(str.substring(0,str.length-1));
        }
    });

 

solok.js

(function(window, $, undefined) {
    var Solok = window.Solok = {
        __ctx: null,
        error: function() {
            if (window.console) {
                window.console.error.apply(window.console, arguments);
            } else {
                var first = "(Solok.error)";
                if (arguments.length > 0) {
                    first = arguments[0];
                }
                alert(first);
            }
        }
    };
    function getCtx() {
        // 0. test __ctx
        if (Solok.__ctx !== null) {
            return Solok.__ctx;
        }
        // 1. test  /ctx/html/...  or  /html/...
        var pathname = window.location.pathname, pathre = /^(///w+)?//html//.*$/;
        var match = pathname.match(pathre);
        if (match != null) {
            return match[1] == undefined ? "" : match[1];
        }
        // 2. test /solok(t)../...
        var pathre2 = /^(//solo(?:k|t)/w+)//.*$/i;
        match = pathname.match(pathre2);
        if (match != null) {
            return match[1];
        }
        Solok.error("can't find ctx.");
        return "";
    };
    $.extend(Solok, {
        ctx: getCtx(),
        emptyFn : function() {},
        type : function(value) {
            if (value === null)
                return 'null';
            var type = typeof value;
            if (type === 'undefined' || type === 'string' || type === 'number' || type === 'boolean') {
                return type;
            }
            var typeToString = toString.call(value);
            switch (typeToString) {
            case '[object Array]':
                return 'array';
            case '[object Date]':
                return 'date';
            case '[object Boolean]':
                return 'boolean';
            case '[object Number]':
                return 'number';
            case '[object RegExp]':
                return 'regexp';
            }
            if (type === 'function') {
                return 'function';
            }
            if (type === 'object') {
                if (value.nodeType !== undefined) {
                    if (value.nodeType === 3) {
                        return (//S/).test(value.nodeValue) ? 'textnode' : 'whitespace';
                    }
                    else {
                        return 'element';
                    }
                }
                return 'object';
            }
        },
        isArray : function(value) {
            return Solok.type(value) === 'array';
        },
        addScript: function(url, onloadCallback, scope) {
            onloadCallback = onloadCallback || Solok.util.emptyFn;
            var script = document.createElement('script'),
                onload = function() {
                    script.onload = null;
                    return onloadCallback.call(scope, url);
                };
            script.type = 'text/javascript';
            script.src = url;
            script.onload = onload;
            $('head')[0].appendChild(script);
        }
    });
   
    Solok.util = {
        fromQueryString : function(queryString) {
            var parts = queryString.replace(/^/?/, '').split('&'), result = {},
                i, len, part, components, name, value;
            for (i = 0, len = parts.length; i < len; ++i) {
                part = parts[i];
                if (part.length > 0) {
                    components = part.split('=');
                    name = decodeURIComponent(components[0]);
                    value = (components[1] !== undefined) ? decodeURIComponent(components[1]) : '';
                    if (result.hasOwnProperty(name)) {
                        if (!Solok.isArray(result[name])) {
                            result[name] = [result[name]];
                        }
                        result[name].push(value);
                    } else {
                        result[name] = value;
                    }
                }
            }
            return result;
        },
        onlyNum:function(event) {
            if(!((event.keyCode>=48&&event.keyCode<=57)||(event.keyCode>=96&&event.keyCode<=105))){
                return false;
            }
            return true;
        },
        /**
         * 成功等提示 使用方法:引入此js文件,
         * 若成功时调用:Solok.util.showMessage("成功提示信息",true,$element);
         * 若失败时调用:Solok.util.showMessage("失败提示信息",false,$element);
         *
         * @author wgl
         * @param message 提示信息
         * @param status 状态,成功为true,失败为false 默认false
         * @param $element 提示放置element
         */
        showMessage:function(message,status,$element){
            var successDivClass = "color1";
            var wrongDivClass = "color3";
            var successSpanClass = "pop_bar_success";
            var wrongSpanClass = "pop_bar_warm";
            // 若未指明元素,则不进行提示
            if ($element == undefined || $element == null) {
                return false;
            }
            // 先清空提示信息
            $element.html("");
            // 若无提示信息,不进行提示
            if (message == undefined || message == null) {
                return false;
            }
            // 状态默认为false 失败
            if (status != true) {
                status = false;
            }
            var div = $("<div />").addClass("pop_bar");
            // 默认失败提示
            div.removeClass(successDivClass).addClass(wrongDivClass);
            // 若成功则赋给成功class
            if (status) {
                div.removeClass(wrongDivClass).addClass(successDivClass);
            }
            var b1 = $("<b />").addClass("b1");
            div.append(b1);
            var b2 = $("<b />").addClass("b2");
            div.append(b2);
            var b3 = $("<b />").addClass("b3");
            div.append(b3);
            var b4 = $("<b />").addClass("b4");
            div.append(b4);
            var contentCornerDiv = $("<div />").addClass("contentCorner");
            div.append(contentCornerDiv);
            var innerDiv = $("<div />");
            contentCornerDiv.append(innerDiv);
            var span = $("<span />");
            // 默认失败提示
            span.removeClass(successSpanClass).addClass(wrongSpanClass);
            // 若成功则赋给成功class
            if (status) {
                span.removeClass(wrongSpanClass).addClass(successSpanClass);
            }
            innerDiv.append(span);
            span.append(message);
            var b5 = $("<b />").addClass("b5");
            div.append(b5);
            var b6 = $("<b />").addClass("b6");
            div.append(b6);
            var b7 = $("<b />").addClass("b7");
            div.append(b7);
            var b8 = $("<b />").addClass("b8");
            div.append(b8);
//                var successHtml = '<div class="pop_bar color1">'+
//                                    '<b class="b1"></b><b class="b2"></b><b class="b3"></b><b class="b4"></b>'+
//                                    '<div class="contentCorner">'+
//                                        '<div>'+
//                                            '<span class="'+classname+'">'+message+' </span>'+
//                                        '</div>'+
//                                    '</div>'+
//                                    '<b class="b5"></b><b class="b6"></b><b class="b7"></b><b class="b8"></b>'+
//                                '</div>';
//                $element.addClass("pop_barP").append(successHtml);
            $element.addClass("pop_barP").append(div);
        }
    };
    // 系统级业务代码----------------------------
    Solok.sys = {
        // 当前用户是否已经登入(弱判断,不要依赖于这个结论做重要的操作)
        isOnline: function() {
            var cookieValue = null, name = "SOLOK_USER";
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = $.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue != null;
        }
    };
    // 业务代码-----------------------------------------------
    /**
     * 优惠卷功能
     */
    Solok.coupon = {
        /**
         * 收藏优惠卷
         */
        storeCoupon : function(couponId, merchantId,date,img, options) {
            options = $.extend(true, {
                success : Solok.emptyFn,
                error : Solok.emptyFn
            }, options);
            $.ajax({
                type : "POST",
                url : "cus_save",
                data : {
                    "couponid" : couponId,
                    "merchantid" : merchantId,
                    "imgName" : img,
                    "expireDate":date
                },
                dataType : "json",
                success : function(data) {
                    if (data.code == 0) {
                        options.success(null, data);
                    } else {
                        options.error(null, data);
                    }
                },
                error : options.error
            });
        }
    };
   
    Solok.share = {
        setShareHref : function(name,currentwindowurl,picfullpath){
            var shareHref = "";
            switch(name){
                case "tsina":
//                    新浪微博
                    shareHref = "http://v.t.sina.com.cn/share/share.php?url="
                        // 这个url是当前页面的地址,加上rid=''构成的.
//                        +encodeURIComponent("http://localhost:8080/solok/index.html?rid=123456789")
                        +encodeURIComponent(currentwindowurl)
                        +"&title="+encodeURIComponent("刷乐网:")+"&content=utf-8"
                        +"&pic="
//                        +encodeURIComponent('http://localhost:8080/solok/images/client.jpg'); 现在我们的图片在公网上运行不了
//                        +encodeURIComponent("http://www.2cto.com/uploadfile/2012/0425/20120425094319871.jpg"); 可以运行的例子
                        +encodeURIComponent(picfullpath);
                break;
               
                case "tqq":
//                    腾讯微博
                    shareHref = "http://v.t.qq.com/share/share.php?url="
                        +encodeURIComponent("刷乐网:"+currentwindowurl)
//                        +"&content="+encodeURIComponent("刷乐网: http://localhost:8080/solok/index.html?rid=123456789")
//                        +"&pic="+encodeURIComponent("http://www.2cto.com/uploadfile/2012/0425/20120425094319871.jpg");
                        +"&pic="+encodeURIComponent(picfullpath);
                break;
               
                case "qzone":
//                    qq空间  只需要把店面详细页地址放在encodeURIComponent里面,在地址后面并且加上&rid=""既可以了。
                    shareHref = "http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url="
//                        +encodeURIComponent("http://www.nipic.com/show/2/34/b81d0613ef66f4a6.html?rid=12312312312")
                        +encodeURIComponent(currentwindowurl) // 现在使用上面的,因为 qq空间发博客,必须需要一个公网上的网址
                        +"&title="+encodeURIComponent("刷乐网:"+currentwindowurl)+"&content=utf-8";
//                    window.open('http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+encodeURIComponent(document.location.href));return false; 58团购网
                break;
               
                case "renren":
//                    人人网
                    shareHref = "http://share.renren.com/share/buttonshare.do?link="
//                        +encodeURIComponent("http://www.nipic.com/show/2/34/b81d0613ef66f4a6.html?rid=12312312312")
                        +encodeURIComponent(currentwindowurl)  // 现在使用上面的,因为 人人网发博客,必须需要一个公网上的网址
                        +"&title="+encodeURIComponent("刷乐网:"+currentwindowurl)+"&content=utf-8";
                break;
               
                case "kaixin":
                    shareHref = 'http://www.kaixin001.com/repaste/bshare.php?'
                        +"rtitle="+encodeURIComponent("刷乐网:"+currentwindowurl)
                        +"&rcontent="+encodeURIComponent(currentwindowurl);
                break;
               
                case "n163":
                    shareHref = 'http://t.163.com/article/user/checkLogin.do?'
                        +"link=http://www.solok.com"
                        +"&source="+encodeURIComponent("刷乐网:"+currentwindowurl)
                        +"&info="+encodeURIComponent(currentwindowurl);
                break;   
            }
            return shareHref;
        }
    };
   
})(window, window.jQuery);


摘自 werewr342352321df的专栏