/**
* Created by shanruo on 16-03-10.
*/ (function ( $ ){ $.extend ({ /*
* 根据参数名获取参数 @name String 参数名
*/
queryString : function ( name ){
name = name.replace (/[\[]/, "\\\[" ).replace (/[\]]/, "\\\]" );
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp (regexS );
var results = regex.exec (window.location.search );
if (results == null) {
return "";
}
else {
return decodeURIComponent (results[1].replace (/\+/g, " " ) );
}
}, /*
* 根据URL地址后退 @URL String 需返回的后退URL地址
*/
goBack : function ( url ){
if (url !== "" && typeof url !== "undefined") {
var parameters = window.location.search;
url = url + parameters;
window.location.href = url;
}
else {
window.history.back ();
}
}, // layer 加载层
loading : function ( msg ){
if (!msg) {
msg = '数据加载中...';
}
$.layer ({
time : 0, // 显示多久
shade : [ 0.3, '#000', true ],
loading : {
type : 2,
msg : msg
}, // loadGif 获取样式值,只能为0-3
border : [ 10, 0.3, '#000', true ],
type : 3,
title : [ '', false ],
closeBtn : [ 0, false ]
} );
}, // 关闭loding层
loadClose : function ( ){
layer.loadClose ();
}, clearQuery : function ( obj ){
// 清除条件
$ (obj ).find ("input[type='text']" ).val ("" ); // 日期清除条件
var dateInputObj = $ (obj ).find ("input[type='hidden']" );
dateInputObj.each (function ( ){
var self = $ (this );
if ($ (self ).attr ("isDate" ) === "true") {
var dateInputId = self.attr ("name" ).toString ().substring (3 );
$ ("input[name='"+dateInputId+"']").val (self.val () );
} } ) // 下拉列表选择第一个
$ (obj ).find ("select" ).each (function ( ){
$ (this ).find ("option:first" ).attr ("selected", "selected" )
} );
}, /*
* 回填文本框值 @objId String 回填对象 @queryStringName 回填参数值
*/
fillInputValue : function ( objId, queryStringName ){
if (objId === "" || typeof objId === "undefined") { return false; } if (queryStringName === "" || typeof queryStringName === "undefined") { return false; } var self = $ ("#" + objId );
var value = $.queryString (queryStringName );
if (self.is ('select' )) {
$ (self ).val (value )
}
if (self.attr ("type" ) === "text") {
$ (self ).val (value )
}
}, /*
* 根据URL跳转到前一页 @URL String 需返回的前一页URL地址 @urlParam json参数数据如:{parame1:"1",parame2:"2"}
*/
goForward : function ( url, urlParam ){
if (typeof urlParam === "undefined" || urlParam === null) {
urlParam = "";
} var pageIndex = $ ("#_MvcPager_Ctrl0_pib" ).val ();
var positionY = $ (document ).scrollTop ();
$.extend (urlParam, {
"py" : positionY,
"pageIndex" : pageIndex
} );
var strPara = $.param (urlParam );
window.location.href = url + "&" + strPara;
}, /*
* 渲染返回行需要表示的行 @lineObj object 行索引对象
*/
animateLine : function ( lineObj ){
var py = $.queryString ("py" );
py = parseFloat (py );
var self = lineObj;
if (typeof lineObj !== Object) {
self = $ ("#" + lineObj );
}
// window.scrollTo(px, py);
$ ("html,body" ).animate ({
scrollTop : py
}, 1 );
self.css ("background", "#ccc" );
}, /*
* 日期比较函数 @startDate String 起始日期 @endDate String 截止日期 @crossMonthNum int 表示不能跨月数(包含)1表示不能跨1个月,小于等于0或者其他都不做判断. @keyWords String 关键字,重新拼接提示信息,默认"开始日期不能为空!";例:查询:重新定义为"查询开始日期不能为空!".
*/
compareDate : function ( startDate, endDate, crossMonthNum, keyWords ){
var start, // 起始时间
end, // 截止日期
yearNum, // 跨年数
monthNum, // 跨月数
strMonthNum, // 拼接语句,提示多少月
crossMonthMsg, // 提示语句
startYear, // 起始日期年份
endYear, // 截止日期年份
startMonth, // 起始日期月份
endMonth, // 截止日期年份
ReturnMsg = "", // 返回提示信息
ReturnMonthMsg = ""; // 返回跨月提示信息 if (startDate.replace (/(^\s*)|(\s*$)/g, "" ) === "") {
crossMonthMsg = "开始日期不能为空!";
if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; }
return crossMonthMsg;
}
if (endDate.replace (/(^\s*)|(\s*$)/g, "" ) === "") {
crossMonthMsg = "结束日期不能为空!";
if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; }
return crossMonthMsg;
} start = new Date (Date.parse (startDate.replace (/-/g, "/" ) ) );
end = new Date (Date.parse (endDate.replace (/-/g, "/" ) ) );
if (end < start) {
crossMonthMsg = "开始日期不能大于结束日期!";
if (keyWords !== "" && typeof keyWords !== "undefined") { return ReturnMsg = keyWords + crossMonthMsg; }
return crossMonthMsg;
} if (typeof crossMonthNum !== "number") {
crossMonthNum = 0;
} if (crossMonthNum !== "" && typeof keyWords !== "undefined") {
crossMonthNum = parseInt (crossMonthNum, 10 );
strMonthNum = crossMonthNum === 1 ? "" : crossMonthNum + "个";
crossMonthMsg = "查询时间不能跨" + strMonthNum + "月!";
if (keyWords !== "" && typeof keyWords !== "undefined") {
ReturnMonthMsg = keyWords + "时间不能跨" + strMonthNum + "月!";
}
else {
ReturnMonthMsg = crossMonthMsg;
} if (crossMonthNum < 12 && crossMonthNum > 0) {
yearNum = 0;
monthNum = crossMonthNum;
} if (crossMonthNum >= 12) {
yearNum = Math.round (crossMonthNum / 12 );
monthNum = Math.abs (crossMonthNum % 12 );
} startMonth = start.getMonth ();
endMonth = end.getMonth ();
startYear = start.getYear ();
endYear = end.getYear ();
/*
* yearNum 为0表示没有跨年按照跨月处理判断 为1表示跨年,按照跨年处理判断
*/
if (yearNum === 0) {
if (endYear - startYear === yearNum) {
if (endMonth - startMonth > monthNum - 1) { return ReturnMonthMsg; }
}
else if (endYear - startYear === yearNum + 1) {
if ((endMonth + 12) - startMonth > monthNum - 1) { return ReturnMonthMsg; }
}
else {
return ReturnMonthMsg;
}
}
else if (yearNum >= 1) {
if (endYear - startYear === yearNum) {
if (endMonth - startMonth > monthNum - 1) { return ReturnMonthMsg; }
}
else if (endYear === startYear) {
return ReturnMsg;
}
else {
return ReturnMonthMsg;
}
}
} return ReturnMsg;
}, /*
* 数组对象转换成Json对象 @obj object form对象
*/
serializeJson : function ( obj ){
var self = $ (obj );
var serializeObj = {};
var array = self.serializeArray ();
var str = self.serialize ();
$ (array ).each (function ( ){
this.value = filterKeyWord (this.value );
if (serializeObj[this.name]) {
if ($.isArray (serializeObj[this.name] )) {
serializeObj[this.name].push (this.value );
}
else {
serializeObj[this.name] = [ serializeObj[this.name], this.value ];
}
}
else {
serializeObj[this.name] = this.value;
}
} ); return serializeObj;
}, /*
* Json对象转换成json字符串 @obj object form对象
*/
jsonToString : function ( obj ){
var parameters = $ (obj )[0];
var arrResult = [];
for ( var key in parameters) {
var parValue = parameters[key]; if (parValue === null || parValue === undefined) {
continue;
}
parValue = filterKeyWord (parValue ); // 过滤"\"以免json序列化问题 arrResult.push ('"' + key + '":"' + parValue + '"' );
} var json = '{' + arrResult.join (',' ) + '}';
return json;
}, /**
* 文本框框正则验证
*
* @regType AZ:只能为字母, 09AZ:只能数字字母, 0.9:只能数字或小数,09:只能为整数
* @arrInputValidate 要验证的文本框数组如['validateInput1','validateInput2']
* @zero 最小值
* @max 最大值
*/
formatRegInput : function ( regType, arrInputValidate, zero, max ){
formatInputCommon (regType, arrInputValidate, zero, max );
}
} ); // 转义"\"特殊字符
function filterKeyWord ( strKeyWord ){
if (strKeyWord.indexOf ("\\" ) === -1) { return strKeyWord; } var str = strKeyWord.split ("\\" );
var len = str.length;
var arr = [];
for (var i = 0; i < len; i++) {
var keyValue = str[i];
arr.push (keyValue );
} return arr.join ("\\\\" );
} /* * 验证控件
*/
function formatInputCommon ( regType, arrInputValidate, zero, max ){
var reg = "";
if (regType.toUpperCase () === "AZ") {
reg = /[^(a-zA-Z)+$]/g;
}
else if (regType.toUpperCase () === "09AZ") {
reg = /[^(0-9a-zA-Z)+$]/g;
}
else if (regType === "0.9") {
reg = /[^(0-9)\.+$]/g;
}
else if (regType === "09") {
reg = /[^(0-9)+$]/g;
}
else {
return;
} var validateObj = [];
for (var i = 0; i < arrInputValidate.length; i++) {
validateObj.push ("input[name='" + arrInputValidate[i] + "']" );
} $ (validateObj.join ("," ) ).on ('keyup', function ( ){
var value = $ (this ).val ();
value = value.toUpperCase ().replace (reg, '' );
if (parseInt (value, 10 ) > max) {
value = max;
}
$ (this ).val (value );
} ); $ (validateObj.join ("," ) ).on ('blur', function ( ){
var value = $ (this ).val ();
value = value.toUpperCase ().replace (reg, '' );
if (zero === 0) {
if (value === "") {
value = 0;
}
} $ (this ).val (value );
} );
}
; }) (jQuery )
05-11 17:46