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);
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。