1 /** 2 * Created by shanruo on 16-03-10. 3 */ 4 5 (function ( $ ){ 6 7 $.extend ({ 8 9 /* 10 * 根据参数名获取参数 @name String 参数名 11 */ 12 queryString : function ( name ){ 13 name = name.replace (/[\[]/, "\\\[" ).replace (/[\]]/, "\\\]" ); 14 var regexS = "[\\?&]" + name + "=([^&#]*)"; 15 var regex = new RegExp (regexS ); 16 var results = regex.exec (window.location.search ); 17 if (results == null) { 18 return ""; 19 } 20 else { 21 return decodeURIComponent (results[1].replace (/\+/g, " " ) ); 22 } 23 }, 24 25 /* 26 * 根据URL地址后退 @URL String 需返回的后退URL地址 27 */ 28 goBack : function ( url ){ 29 if (url !== "" && typeof url !== "undefined") { 30 var parameters = window.location.search; 31 url = url + parameters; 32 window.location.href = url; 33 } 34 else { 35 window.history.back (); 36 } 37 }, 38 39 // layer 加载层 40 loading : function ( msg ){ 41 if (!msg) { 42 msg = '数据加载中...'; 43 } 44 $.layer ({ 45 time : 0, // 显示多久 46 shade : [ 0.3, '#000', true ], 47 loading : { 48 type : 2, 49 msg : msg 50 }, // loadGif 获取样式值,只能为0-3 51 border : [ 10, 0.3, '#000', true ], 52 type : 3, 53 title : [ '', false ], 54 closeBtn : [ 0, false ] 55 } ); 56 }, 57 58 // 关闭loding层 59 loadClose : function ( ){ 60 layer.loadClose (); 61 }, 62 63 clearQuery : function ( obj ){ 64 // 清除条件 65 $ (obj ).find ("input[type='text']" ).val ("" ); 66 67 // 日期清除条件 68 var dateInputObj = $ (obj ).find ("input[type='hidden']" ); 69 dateInputObj.each (function ( ){ 70 var self = $ (this ); 71 if ($ (self ).attr ("isDate" ) === "true") { 72 var dateInputId = self.attr ("name" ).toString ().substring (3 ); 73 $ ("input[name='"+dateInputId+"']").val (self.val () ); 74 } 75 76 } ) 77 78 // 下拉列表选择第一个 79 $ (obj ).find ("select" ).each (function ( ){ 80 $ (this ).find ("option:first" ).attr ("selected", "selected" ) 81 } ); 82 }, 83 84 /* 85 * 回填文本框值 @objId String 回填对象 @queryStringName 回填参数值 86 */ 87 fillInputValue : function ( objId, queryStringName ){ 88 if (objId === "" || typeof objId === "undefined") { return false; } 89 90 if (queryStringName === "" || typeof queryStringName === "undefined") { return false; } 91 92 var self = $ ("#" + objId ); 93 var value = $.queryString (queryStringName ); 94 if (self.is ('select' )) { 95 $ (self ).val (value ) 96 } 97 if (self.attr ("type" ) === "text") { 98 $ (self ).val (value ) 99 } 100 }, 101 102 /* 103 * 根据URL跳转到前一页 @URL String 需返回的前一页URL地址 @urlParam json参数数据如:{parame1:"1",parame2:"2"} 104 */ 105 goForward : function ( url, urlParam ){ 106 if (typeof urlParam === "undefined" || urlParam === null) { 107 urlParam = ""; 108 } 109 110 var pageIndex = $ ("#_MvcPager_Ctrl0_pib" ).val (); 111 var positionY = $ (document ).scrollTop (); 112 $.extend (urlParam, { 113 "py" : positionY, 114 "pageIndex" : pageIndex 115 } ); 116 var strPara = $.param (urlParam ); 117 window.location.href = url + "&" + strPara; 118 }, 119 120 /* 121 * 渲染返回行需要表示的行 @lineObj object 行索引对象 122 */ 123 animateLine : function ( lineObj ){ 124 var py = $.queryString ("py" ); 125 py = parseFloat (py ); 126 var self = lineObj; 127 if (typeof lineObj !== Object) { 128 self = $ ("#" + lineObj ); 129 } 130 // window.scrollTo(px, py); 131 $ ("html,body" ).animate ({ 132 scrollTop : py 133 }, 1 ); 134 self.css ("background", "#ccc" ); 135 }, 136 137 /* 138 * 日期比较函数 @startDate String 起始日期 @endDate String 截止日期 @crossMonthNum int 表示不能跨月数(包含)1表示不能跨1个月,小于等于0或者其他都不做判断. @keyWords String 关键字,重新拼接提示信息,默认"开始日期不能为空!";例:查询:重新定义为"查询开始日期不能为空!". 139 */ 140 compareDate : function ( startDate, endDate, crossMonthNum, keyWords ){ 141 var start, // 起始时间 142 end, // 截止日期 143 yearNum, // 跨年数 144 monthNum, // 跨月数 145 strMonthNum, // 拼接语句,提示多少月 146 crossMonthMsg, // 提示语句 147 startYear, // 起始日期年份 148 endYear, // 截止日期年份 149 startMonth, // 起始日期月份 150 endMonth, // 截止日期年份 151 ReturnMsg = "", // 返回提示信息 152 ReturnMonthMsg = ""; // 返回跨月提示信息 153 154 if (startDate.replace (/(^\s*)|(\s*$)/g, "" ) === "") { 155 crossMonthMsg = "开始日期不能为空!"; 156 if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; } 157 return crossMonthMsg; 158 } 159 if (endDate.replace (/(^\s*)|(\s*$)/g, "" ) === "") { 160 crossMonthMsg = "结束日期不能为空!"; 161 if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; } 162 return crossMonthMsg; 163 } 164 165 start = new Date (Date.parse (startDate.replace (/-/g, "/" ) ) ); 166 end = new Date (Date.parse (endDate.replace (/-/g, "/" ) ) ); 167 if (end < start) { 168 crossMonthMsg = "开始日期不能大于结束日期!"; 169 if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; } 170 return crossMonthMsg; 171 } 172 173 if (typeof crossMonthNum !== "number") { 174 crossMonthNum = 0; 175 } 176 177 if (crossMonthNum !== "" && typeof keyWords !== "undefined") { 178 crossMonthNum = parseInt (crossMonthNum, 10 ); 179 strMonthNum = crossMonthNum === 1 ? "" : crossMonthNum + "个"; 180 crossMonthMsg = "查询时间不能跨" + strMonthNum + "月!"; 181 if (keyWords !== "" && typeof keyWords !== "undefined") { 182 ReturnMonthMsg = keyWords + "时间不能跨" + strMonthNum + "月!"; 183 } 184 else { 185 ReturnMonthMsg = crossMonthMsg; 186 } 187 188 if (crossMonthNum < 12 && crossMonthNum > 0) { 189 yearNum = 0; 190 monthNum = crossMonthNum; 191 } 192 193 if (crossMonthNum >= 12) { 194 yearNum = Math.round (crossMonthNum / 12 ); 195 monthNum = Math.abs (crossMonthNum % 12 ); 196 } 197 198 startMonth = start.getMonth (); 199 endMonth = end.getMonth (); 200 startYear = start.getYear (); 201 endYear = end.getYear (); 202 /* 203 * yearNum 为0表示没有跨年按照跨月处理判断 为1表示跨年,按照跨年处理判断 204 */ 205 if (yearNum === 0) { 206 if (endYear - startYear === yearNum) { 207 if (endMonth - startMonth > monthNum - 1) { return ReturnMonthMsg; } 208 } 209 else if (endYear - startYear === yearNum + 1) { 210 if ((endMonth + 12) - startMonth > monthNum - 1) { return ReturnMonthMsg; } 211 } 212 else { 213 return ReturnMonthMsg; 214 } 215 } 216 else if (yearNum >= 1) { 217 if (endYear - startYear === yearNum) { 218 if (endMonth - startMonth > monthNum - 1) { return ReturnMonthMsg; } 219 } 220 else if (endYear === startYear) { 221 return ReturnMsg; 222 } 223 else { 224 return ReturnMonthMsg; 225 } 226 } 227 } 228 229 return ReturnMsg; 230 }, 231 232 233 /* 234 * 数组对象转换成Json对象 @obj object form对象 235 */ 236 serializeJson : function ( obj ){ 237 var self = $ (obj ); 238 var serializeObj = {}; 239 var array = self.serializeArray (); 240 var str = self.serialize (); 241 $ (array ).each (function ( ){ 242 this.value = filterKeyWord (this.value ); 243 if (serializeObj[this.name]) { 244 if ($.isArray (serializeObj[this.name] )) { 245 serializeObj[this.name].push (this.value ); 246 } 247 else { 248 serializeObj[this.name] = [ serializeObj[this.name], this.value ]; 249 } 250 } 251 else { 252 serializeObj[this.name] = this.value; 253 } 254 } ); 255 256 return serializeObj; 257 }, 258 259 /* 260 * Json对象转换成json字符串 @obj object form对象 261 */ 262 jsonToString : function ( obj ){ 263 var parameters = $ (obj )[0]; 264 var arrResult = []; 265 for ( var key in parameters) { 266 var parValue = parameters[key]; 267 268 if (parValue === null || parValue === undefined) { 269 continue; 270 } 271 parValue = filterKeyWord (parValue ); // 过滤"\"以免json序列化问题 272 273 arrResult.push ('"' + key + '":"' + parValue + '"' ); 274 } 275 276 var json = '{' + arrResult.join (',' ) + '}'; 277 return json; 278 }, 279 280 /** 281 * 文本框框正则验证 282 * 283 * @regType AZ:只能为字母, 09AZ:只能数字字母, 0.9:只能数字或小数,09:只能为整数 284 * @arrInputValidate 要验证的文本框数组如['validateInput1','validateInput2'] 285 * @zero 最小值 286 * @max 最大值 287 */ 288 formatRegInput : function ( regType, arrInputValidate, zero, max ){ 289 formatInputCommon (regType, arrInputValidate, zero, max ); 290 } 291 } ); 292 293 // 转义"\"特殊字符 294 function filterKeyWord ( strKeyWord ){ 295 if (strKeyWord.indexOf ("\\" ) === -1) { return strKeyWord; } 296 297 var str = strKeyWord.split ("\\" ); 298 var len = str.length; 299 var arr = []; 300 for (var i = 0; i < len; i++) { 301 var keyValue = str[i]; 302 arr.push (keyValue ); 303 } 304 305 return arr.join ("\\\\" ); 306 } 307 308 /*309 * 验证控件 310 */ 311 function formatInputCommon ( regType, arrInputValidate, zero, max ){ 312 var reg = ""; 313 if (regType.toUpperCase () === "AZ") { 314 reg = /[^(a-zA-Z)+$]/g; 315 } 316 else if (regType.toUpperCase () === "09AZ") { 317 reg = /[^(0-9a-zA-Z)+$]/g; 318 } 319 else if (regType === "0.9") { 320 reg = /[^(0-9)\.+$]/g; 321 } 322 else if (regType === "09") { 323 reg = /[^(0-9)+$]/g; 324 } 325 else { 326 return; 327 } 328 329 var validateObj = []; 330 for (var i = 0; i < arrInputValidate.length; i++) { 331 validateObj.push ("input[name='" + arrInputValidate[i] + "']" ); 332 } 333 334 $ (validateObj.join ("," ) ).on ('keyup', function ( ){ 335 var value = $ (this ).val (); 336 value = value.toUpperCase ().replace (reg, '' ); 337 if (parseInt (value, 10 ) > max) { 338 value = max; 339 } 340 $ (this ).val (value ); 341 } ); 342 343 $ (validateObj.join ("," ) ).on ('blur', function ( ){ 344 var value = $ (this ).val (); 345 value = value.toUpperCase ().replace (reg, '' ); 346 if (zero === 0) { 347 if (value === "") { 348 value = 0; 349 } 350 } 351 352 $ (this ).val (value ); 353 } ); 354 } 355 ; 356 357 }) (jQuery )