jquery验证后ajax提交,返回消息如何统一显示的问题

/* jquery验证后ajax提交,返回消息如何跟jquery验证体系统一显示的问题,网上查了很多资料,都没有找到明确的答案,通过数小时的尝试,终于解决了,现举一个简单的例子,给需要的人参考参考吧*/
<script type="text/javascript">
    $(function () {
        var tt = $("#fmLogin").validate({
            submitHandler: function () {
                $.ajax({
                    url: "http://localhost:6633/Account/AjaxLogin",
                    cache: false,
                    type: "GET",
                    dataType: 'jsonp',
                    data: $("#fmLogin").serialize(),
                    success: function (result) {
                        if (!result) {
                            var errormap = { SystemMsg: "系统忙,请稍后再试!" }; //注意,map里的key,必须是页面相关标签的name属性值
                            tt.showErrors(errormap, tt.errorlist);                //list就是rules里面定义的那些,注意rules必须包含上面map里的key
                        }
                        if (result.Msg.Code == "VerifyCode") {
                            var errormap = { VerifyCode: result.Msg.Message };
                            tt.showErrors(errormap, tt.errorlist);
                        }
                    }
                });
            },
            errorPlacement: function (error, element) {
                error.css("top", element.top + element.height);
                error.css("left", element.left);
                if (element.is("#txtVerifyCode"))
                    error.insertAfter(element.parent());
                else
                    error.insertAfter(element);
            },
            rules: {
                Account: {
                    required: true,
                    minlength: 6,
                    maxlength: 20,
                },
                Password: {
                    required: true,
                    minlength: 6,
                    maxlength: 15
                },
                VerifyCode: {
                    required: true
                },
                SystemMsg: {    //如果要用来绑定消息,就必须定义,哪怕什么验证都不做,注意必须是绑定消息的某标签的name属性值
                }
            }
        });
    });
</script>

posted on 2015-06-27 18:08  丰云  阅读(90)  评论(0)    收藏  举报

导航