Jquery 自定义插件写法(示例)
(function ($) { $.SmsHelper = $.SmsHelper || {}; $.extend($.SmsHelper, { //插件具体实现代码 yzmnum: 60, AjaxUrl: "/AjaxMethod/CommonSMS.ashx?key=GetSmsCode", GetSmsCodeClick: function (phoneCode, obj) { if (phoneCode == '') { $.AmHelper.ShowMsg('请输入手机号'); return; } var reg = /^1[358]\d{9}$/; if (!reg.test(phoneCode)) { $.AmHelper.ShowMsg("手机号码格式不正确!"); return; } var objText = $(obj).val(); if (objText == '') { objText = $(obj).text(); if (objText.indexOf('验证码') > -1) { $.SmsHelper.GetSmsCodeEx(phoneCode, obj); } } else { if (objText.indexOf('验证码') > -1) { $.SmsHelper.GetSmsCode(phoneCode, obj); } } }, //获得手机验证码 GetSmsCode: function (mobile, obj) { $(obj).val('请稍后...' + $.SmsHelper.yzmnum); $(obj).attr("disabled", true); var yzmtime = setInterval(function () { $.SmsHelper.yzmnum--; $(obj).val('请稍后...' + $.SmsHelper.yzmnum); if ($.SmsHelper.yzmnum == 0) { $(obj).val('获取验证码'); $(obj).attr("disabled", false); clearInterval(yzmtime); $.SmsHelper.yzmnum = 60; } }, 1000); if (mobile != "") { $.ajax({ type: "post", url: $.SmsHelper.AjaxUrl, data: { "Mobile": mobile, "PageType": $.SmsHelper.smsPageType }, async: false, dataType: "json", success: function (msg) { if (msg.returncode == "0") { $.AmHelper.ShowMsg("验证码短信已发送"); } else { $.AmHelper.ShowMsg(msg.message); } }, complete: function (XHR, TS) { XHR = null } }); } else { $.AmHelper.ShowMsg('请填写手机号码!'); } }, GetSmsCodeEx: function (mobile, obj, pageType) { $(obj).text('请稍后...' + $.SmsHelper.yzmnum); $(obj).attr("disabled", true); var yzmtime = setInterval(function () { $.SmsHelper.yzmnum--; $(obj).text('请稍后...' + $.SmsHelper.yzmnum); if ($.SmsHelper.yzmnum == 0) { $(obj).text('获取验证码'); $(obj).attr("disabled", false); clearInterval(yzmtime); $.SmsHelper.yzmnum = 60; } }, 1000); if (mobile != "") { $.ajax({ type: "post", url: $.SmsHelper.AjaxUrl, data: { "Mobile": mobile, "PageType": smsPageType }, async: false, dataType: "json", success: function (msg) { if (msg.returncode == "0") { $.AmHelper.ShowMsg("验证码短信已发送"); } else { $.AmHelper.ShowMsg(msg.message); } }, complete: function (XHR, TS) { XHR = null } }); } else { $.AmHelper.ShowMsg('请填写手机号码!'); } } }); })(jQuery);
调用方式:
$.SmsHelper.GetSmsCodeClick();