javascript 帮助类Utility

var Utility = (function ($) {


    var _common = {
        getFormData: function (parent_id) {
            var pd = {};
            var setValue = function (that) {
                if (!that) return;


                var name = $(that).attr('name');
                var value = '';
                switch (that.tagName.toLowerCase()) {
                    case 'select':
                        value = $(that).find('option:selected').val();
                        break;
                    case 'input':
                        {
                            var type = $(that).attr('type');
                            switch (type) {
                                case 'checkbox':
                                    value = $(that).prop('checked');
                                    break;
                                case 'hidden':
                                case 'text':
                                    value = $(that).val();
                                    break;
                            }
                        }
                        break;
                    default:
                        value = $(that).val();
                        break;
                }
                if (!!name) {
                    pd[name] = value;
                }
            };


            $('#' + parent_id).find('input,textarea,select').each(function () {
                if (!this) return true;
                setValue(this);
            });
           
            return pd;
        },
        setFormData: function (parent_id, data) {
            var setValue = function (that) {
                var name = $(that).attr('name');
                if (!name) return false;
                var value = data[name];
                if (typeof (value) == 'undefined' || value == null)
                    value = '';
                var tag = that.tagName.toLowerCase();
                switch (tag) {
                    case 'input':
                        {
                            var type = $(that).attr('type');
                            switch (type) {
                                case 'checkbox':
                                    $(that).prop('checked', value);
                                    break;
                                case 'hidden':
                                case 'text':
                                    $(that).val(value);
                                    break;
                            }
                        }
                        break;
                    default:
                        $(that).val(value);
                        break;
                }
            };
            $('#' + parent_id).find('input,textarea,select').each(function () {
                if (!this) return true;
                setValue(this);
            });
        },
        template: function (str, o, regexp) {
            return str.replace(regexp || /\\?\{([^{}]+)\}/g, function (match, name) {
                return (o[name] === undefined) ? '' : o[name];
            });
        },
        postJson: function (url, pd) {
            return $.ajax({
                url: url,
                type: 'POST',
                data: JSON.stringify(pd),
                contentType: "application/json"
            });
        },
        postJsonSync: function (url, pd) {
            return $.ajax({
                url: url,
                type: 'POST',
                async: false,
                data: JSON.stringify(pd),
                contentType: "application/json"
            });
        },
        getRequestParam: function (paras) {
            var url = window.location.href;
            var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
            var paraObj = {}
            for (i = 0; j = paraString[i]; i++) {
                paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
            }
            var returnValue = paraObj[paras.toLowerCase()];
            if (typeof (returnValue) == "undefined") {
                return "";
            } else {
                return returnValue;
            }
        },
    
 
        fillValue: function (rawselector,destselector) {
            $(destselector).find('input[type=text],input[type=hidden],input[type=checkbox],select,textarea').each(function (index, item) {
                if (!item) return false;
                var name = $(item).attr('name');
                if (!!name) {
                    var finds = $(rawselector).find('[name="' + name + '"]');
                    if (finds.length > 0) {
                        var isset = false;
                        switch (item.tagName.toLowerCase()) {
                            case 'input':
                                {
                                    var type = $(item).attr('type');
                                    if (type == 'checkbox') {
                                        $(item).prop('checked', $(finds).val() == '1');
                                        isset = true;
                                    }
                                }
                                break;
                            
                        }
                        if (!isset)
                            $(item).val(finds.val());
                    }
                }
            });
        },
        getFormParam: function (selector) {
            var output = '';


            var func = function (index, item) {
                var name = $(item).attr('id');
                var value = $(item).val();
                if (output.length > 0)
                    output = output + '&';
                switch(item.tagName.toLowerCase())
                {
                    case 'input':
                        {
                            var type = $(item).attr('type');
                            if (type == 'checkbox')
                                value = $(item).prop('checked') ? 1 : 0;
                        }
                        break;
                }
                output = output + name + '=' + value;
            };


            var pattern = 'input[type=text],input[type=hidden],input[type=checkbox],select,textarea';


            if (!!selector) {
                $(selector).find(pattern).each(func);
            }
            else {
                $(pattern).each(func);
            }


            return output;
        },


        //中文大写金额转换:转换为列表,最多可转换到亿
        toAmountInWords: function (val) {
            var output = null;
            do {
                num = parseInt(val);
                if (isNaN(num))
                    break;
                if ((num + '') != (val + '')) {
                    console.warn('传入参数被截断');
                }
                var list = [
                   { value: '', text: '亿' },
                   { value: '', text: '仟' },
                   { value: '', text: '佰' },
                   { value: '', text: '拾' },
                   { value: '', text: '万' },
                   { value: '', text: '仟' },
                   { value: '', text: '佰' },
                   { value: '', text: '拾' },
                   { value: '', text: '元' }
                ];
                var chars = ['壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
                var step = 0;
                var chu = 100000000;
                var temp = num;


                var f = temp / chu;
                var d = 0;
                if (f < 1)
                    d = 0;
                else
                    d = parseInt(f);


                if (!!d) {
                    list[step].value = chars[d - 1];
                }
                step++;
                for (var i = 0; i < 8; ++i) {
                    temp = temp - (d * chu);
                    chu = chu / 10;


                    f = temp / chu;
                    var d = 0;
                    if (f < 1)
                        d = 0;
                    else
                        d = parseInt(f);


                    if (!!d) {
                        if (step > 0)
                            list[step].value = chars[d - 1];
                    }
                    else {
                        if (list[step - 1].value != '') {
                            list[step].value = '零';
                        }
                    }
                    step++;
                }
                output = list;
            } while (false);
            return output;
        },
        getIEVersion: function () {
            //获取IE版本
         
          
            var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
            var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
            var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
            var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
            if (isIE) {
                var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
                reIE.test(userAgent);
                var fIEVersion = parseFloat(RegExp["$1"]);
                if (fIEVersion == 7) {
                    return 7;
                } else if (fIEVersion == 8) {
                    return 8;
                } else if (fIEVersion == 9) {
                    return 9;
                } else if (fIEVersion == 10) {
                    return 10;
                } else {
                    return 6;//IE版本<=7
                }
            } else if (isEdge) {
                return 2000;//edge
            } else if (isIE11) {
                return 11; //IE11  
            } else {
                return 1000;//不是ie浏览器
            }
            
        }
    };


    this.common = _common;
    return this;
})(jQuery);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值