layui 表单ajax验证无效的解决方案

/*
         检查手机号格式,手机号已存在则不合法
         param:mobile-手机号
         return:true-手机号合法,string-不合法返提示
         error:layer.alert()
         */
        chkMobile: function(mobile) {
            var _this = this;
            $res = _this.chkMobFormat(mobile);
            if ($res !== true) return $res;
            $restr="";
            $.ajax({
                type: 'POST',
                url: _this.check_url,
                data: {
                    mobile: mobile
                },
                async: false, //改为同步请求
                dataType: "json",
                success: function(res) {
                    if (res.code === 1) {
                        //return true;
                    } else {
                        $restr="手机号已经存在";
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    _this.showMsg('操作失败!!!' + XMLHttpRequest.status + "|" + XMLHttpRequest.readyState + "|" + textStatus)
                }
            });
            return $restr; //ajax验证,需要async: false ,并且把return翻到ajax外。
        },

 

posted @ 2023-05-14 00:50  djiz  阅读(10)  评论(0编辑  收藏  举报