实用js片段
算法
//加法 add(10,2) //12 function add(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) { c = 0; } try { d = b.toString().split(".")[1].length; } catch (f) { d = 0; } return e = Math.pow(10, Math.max(c, d)), (mul(a, e) + mul(b, e)) / e; } //减法 sub(10,2) //8 function sub(a, b) { var c, d, e; try { c = a.toString().split(".")[1].length; } catch (f) { c = 0; } try { d = b.toString().split(".")[1].length; } catch (f) { d = 0; } return e = Math.pow(10, Math.max(c, d)), (mul(a, e) - mul(b, e)) / e; } //乘法 mul(10,2) //20 function mul(a, b) { var c = 0, d = a.toString(), e = b.toString(); try { c += d.split(".")[1].length; } catch (f) { } try { c += e.split(".")[1].length; } catch (f) { } return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c); } //除法 div(10,2) //5 function div(a, b) { var c, d, e = 0, f = 0; try { e = a.toString().split(".")[1].length; } catch (g) { } try { f = b.toString().split(".")[1].length; } catch (g) { } return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), mul(c / d, Math.pow(10, f - e)); }
验证
// 判断是否是整数 function isInt(num) { var reg = new RegExp("^[0-9]*$"); return reg.test(num); } // 判断是否是数字 function isNum(num) { var reg = new RegExp("^(\-)?[0-9]+(\.[0-9]+)?$"); return reg.test(num); } // 判断是否是手机号码 function isPhone(phone) { var reg = new RegExp("^1[0-9]{10}$"); return reg.test(phone); } //只能输入有两位小数的正实数,多用于价格 function isFloat(num) { var reg = new RegExp("^[0-9]+(\\.[0-9]{1,2})?$"); return reg.test(num); } //只能输入有1位小数的正实数,多用于打折 function isFloat1(num) { var reg = new RegExp("^[0-9]+(\\.[0-9]{1})?$"); return reg.test(num); }
获取当前日期
//获取当前时间年-月-日 getNowTime() //2017-09-23 function getNowTime() { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var currentdate = year + seperator1 + checkNum(month) + seperator1 + checkNum(strDate) return currentdate; } function checkNum(n){ var num; if (n>= 1 && n<= 9) { num= "0" + n; } return num }
jquery 请求
//通用get请求 function commonGet(url, success, complete) { $.ajax({ url: url, type: 'get', success: function (resp) { if (resp.code == 0) { typeof success === "function" && success(resp); } else { console.log(resp); layer.msg(resp.msg); } typeof complete === "function" && complete(resp); }, error: function () { layer.msg('系统错误,请重试!'); typeof complete === "function" && complete(); } }); } //通用post请求 function commonPost(url, data, success, complete) { setTimeout(function () { $(".loading").hide(); },500) $(document).ajaxStart(function () { // layer.load(1, { shade: [0.1,'#fff'] })//0.1透明度的白色背景 $(".loading").show(); }) $(document).ajaxSuccess(function () { $(".loading").hide(); }) $.ajax({ url: url, type: 'post', data: data, success: function (resp) { if (resp.code == 0) { typeof success === "function" && success(resp); } else { // console.log(resp); $(".loading").hide(); layer.msg(resp.msg); } typeof complete === "function" && complete(resp); }, error: function (resp) { $(".loading").hide(); layer.msg('系统错误,请重试!'); typeof complete === "function" && complete(); } }); }
地址参数方法
//http://www.huistd.com/?id=99&ttt=3&haha=33 // GetQueryString("haha") //获取地址栏参数 function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } //changeUrlArg(url, 'haha', 33); // http://www.huistd.com/?id=99&ttt=3&haha=33 //修改地址栏参数 function changeUrlArg(url, arg, val) { var pattern = arg + '=([^&]*)'; var replaceText = arg + '=' + val; return url.match(pattern) ? url.replace(eval('/(' + arg + '=)([^&]*)/gi'), replaceText) : (url.match('[\?]') ? url + '&' + replaceText : url + '?' + replaceText); } //获取字符串长度 function getByteLen(val) { if (val == null || val == "undefined") return 0; return val.length; }
js,form表单数据转化为json格式
//自定义函数处理queryParams的批量增加 jsonArr.serializeJsonObject() $.fn.serializeJsonObject = function () { var json = {}; var form = this.serializeArray(); $.each(form, function () { if (json[this.name]) { if (!json[this.name].push) { json[this.name] = [json[this.name]]; } json[this.name].push(); } else { json[this.name] = this.value || ""; } }); return json; }