如何清除jQuery验证错误 [英] How to clear jquery validate errors

查看:72
本文介绍了如何清除jQuery验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在隐藏现有表单并将其发布到服务器. jQuery validate会执行大部分验证,但是如果服务器上的验证失败,我们会将错误以JSON的形式返回给客户端.

I'm hijaxing an existing form and POSTing to the server. jQuery validate does most of the validation but if validation fails on the server we return the errors to the client as JSON.

下面是执行此操作的代码:

Below is the code that does that:

<script type="text/javascript">

    $(function () {

        $("form").submit(function (e) {
            var $form = $(this);
            var validator = $form.data("validator");

            if (!validator || !$form.valid())
                return;

            e.preventDefault();

            $.ajax({
                url: "@Url.Action("index")",
                type: "POST",
                data: $form.serialize(),
                statusCode: {
                    400: function(xhr, status, err) {                           
                        var errors = $.parseJSON(err);
                        validator.showErrors(errors);
                    }
                },
                success: function() {
                    // clear errors
                    // validator.resetForm();
                    // just reload the page for now
                    location.reload(true);
                }
            });
        });

    });

</script>

问题是,如果POST成功,我似乎无法清除验证错误.我尝试调用validator.resetForm(),但这没什么区别,仍然显示由showError()调用添加的错误消息.

The problem is I can't seem to clear the validation errors if the POST is successful. I've tried calling validator.resetForm() but this makes no difference, the error messages added by the showError() call, are still displayed.

请注意,我也在使用jQuery.validate.unobtrusive插件.

Note I'm also using the jQuery.validate.unobtrusive plugin.

推荐答案

您前不久发布了此信息,我不知道您是否能够解决它?我对jQuery validate和 jQuery.validate.unobtrusive 插件有相同的问题.

You posted this a while ago, I don't know if you managed to solve it? I had the same problem with jQuery validate and the jQuery.validate.unobtrusive plugin.

在检查了源代码并进行了一些调试之后,我得出的结论是,问题出在不引人注目的插件处理错误消息的方式上.它会删除 jQuery.validate 插件设置的errorClass,因此,在重置表单时,jQuery validate无法找到要删除的错误标签.

After examining the source code and some debugging, I came to the conclusion that the problem comes from the way the unobtrusive plugin handles error messages. It removes the errorClass that the jQuery.validate plugin sets, and so when the form is reset, jQuery validate cannot find the error labels to remove.

我不想修改插件的代码,因此我可以通过以下方式克服这一问题:

I did not want to modify the code of the plugins, so I was able to overcome this in the following way:

// get the form inside we are working - change selector to your form as needed
var $form = $("form");

// get validator object
var $validator = $form.validate();

// get errors that were created using jQuery.validate.unobtrusive
var $errors = $form.find(".field-validation-error span");

// trick unobtrusive to think the elements were succesfully validated
// this removes the validation messages
$errors.each(function(){ $validator.settings.success($(this)); })

// clear errors from validation
$validator.resetForm();

注意:我使用$前缀表示变量,以表示包含jQuery对象的变量.

这篇关于如何清除jQuery验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆