/// <reference path="jquery-1.8.3.min.js" /> /*********************************************************************/ /***************************Jquery 扩展****************************/ /*********************************************************************/ jQuery.mIsNull = function (obj) { if (obj == "" || typeof (obj) == "undefined" || obj == null) { return true; } else { return false; } }; jQuery.mCheckNull = function (id, tipid, nullmess, ctype) { var str = $.mGetValue(id, ctype); var tid = ($.mIsNull(tipid)) ? id : tipid; var obj = ($.mIsNull(tipid)) ? $.mTip : $.mTipCustom; if ($.mIsNull(str)) { obj("#" + tid, nullmess, 2); } else { obj("#" + tid, "", 1); } }; jQuery.mCheckNullAndReg = function (id, tipid, nullmess, regmess, ctype, rtype) { var str = $.mGetValue(id, ctype); var tid = ($.mIsNull(tipid)) ? id : tipid; var obj = ($.mIsNull(tipid)) ? $.mTip : $.mTipCustom; if ($.mIsNull(str)) { obj("#" + tid, nullmess, 2); } else { if ($.mCheck(str, rtype)) { obj("#" + tid, "", 1); } else { obj("#" + tid, regmess, 2); } } }; jQuery.mCheck = function (s, type) { var objbool = false; var objexp = ""; switch (type) { case 'money': //金额格式,格式定义为带小数的正数,小数点后最多三位 objexp = "^[0-9]+[\.][0-9]{0,3}$"; break; case 'numletter_': //英文字母和数字和下划线组成 objexp = "^[0-9a-zA-Z\_]+$"; break; case 'numletter': //英文字母和数字组成 objexp = "^[0-9a-zA-Z]+$"; break; case 'numletterchina': //汉字、字母、数字组成 objexp = "^[0-9a-zA-Z\u4e00-\u9fa5]+$"; break; case 'email': //邮件地址格式 objexp = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$"; break; case 'tel': //固话格式 objexp = /^((\d2,3)|(\d{3}\-))?(0\d2,3|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/; break; case 'mobile': //手机号码 objexp = "^(13[0-9]|15[0-9]|18[0-9])([0-9]{8})$"; break; case 'decimal': //浮点数 objexp = "^[0-9]+([.][0-9]+)?$"; break; case 'url': //网址 objexp = "(|https://){0,1}[\w\/\.\?\&\=]+"; break; case 'date': //日期 YYYY-MM-DD格式 objexp = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/; break; case 'int': //整数 objexp = "^[0-9]*[1-9][0-9]*$"; break; case 'int+': //正整数包含0 objexp = "^\\d+$"; break; case 'int-': //负整数包含0 objexp = "^((-\\d+)|(0+))$"; break; case 'china': //中文 objexp = /^[\u0391-\uFFE5]+$/; break; } var re = new RegExp(objexp); if (re.test(s)) { return true; } else { return false; } }; jQuery.mTip = function (o, tip, typepic) { var pic = ""; switch (typepic) { case 0: // loading pic = "/images/publicNew/loading.gif"; break; case 1: // ok pic = "/images/publicNew/right.png"; break; case 2: // error pic = "/images/publicNew/error.png"; break; default: //其他任何值时 pic = "/images/publicNew/onLoad.gif"; break; } var eTip = document.createElement("span"); var objid = $(o).attr("id") + "_tipDiv"; var value = $(o).val(); //绝对路径 var x = $(o).offset().top; var y = $(o).offset().left; var w = $(o).width(); var h = $(o).height(); eTip.setAttribute("id", objid); try { document.body.appendChild(eTip); } catch (e) { } $("#" + objid).hide(); $("#" + objid).css({ top: x, left: y + w + 10, height: h, position: "absolute" }); $("#" + objid).html("<img src=https://www.jb51.net/article/\"" + pic + "https://www.jb51.net/article/\" style=https://www.jb51.net/article/\"vertical-align:bottom;margin-right:5px;\">" + tip); $("#" + objid).show(); }; jQuery.mTipCustom = function (o, tip, typepic) { var pic = ""; switch (typepic) { case 0: // loading pic = "/images/publicNew/loading.gif"; break; case 1: // ok pic = "/images/publicNew/right.png"; break; case 2: // error pic = "/images/publicNew/error.png"; break; default: //其他任何值时 pic = "/images/publicNew/onLoad.gif"; break; } $("#" + o).html("<img src=https://www.jb51.net/article/\"" + pic + "https://www.jb51.net/article/\" style=https://www.jb51.net/article/\"vertical-align:bottom;margin-right:5px;\">" + tip); $("#" + o).show(); }; jQuery.mGetValue = function (controlID, controltype) { var objValue = ""; switch (controltype) { case 'text': //文本输入框 objValue = $.trim($("#" + controlID + "").attr("value")); //取值去左右空格 break; case 'radio': //单选框 objValue = $("input[name='" + controlID + "']:checked").attr("value"); break; case 'select': //下拉列表 objValue = $("#" + controlID + "").attr("value"); break; case 'checkbox': //多选框 $("input[name='" + controlID + "']:checked").each(function () { objValue += $(this).val() + ","; }); break; default: break; } return objValue; }; /** * ajax post提交 * @param url * @param param * @param datat 为html,json,text * @param callback 回调函数 function callBack(data) * @return */ jQuery.mJqAjax = function (url, param, datat, callback) { $.ajax({ type: "post", url: url, data: param, dataType: datat, success: callback, error: function () { } }); };
【经典源码收藏】基于jQuery的项目常见函数封装(2)
内容版权声明:除非注明,否则皆为本站原创文章。