本文介绍了表单重置后,jQuery验证无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下方法重置表单后出现问题:

I'm having an issue after I reset my form using:

$(".cancel").click(function () {
        validator.resetForm();
        $(':input', '#MyForm')
            .not(':button, :submit, :reset, :hidden')
            .val('')
            .removeAttr('checked')
            .removeAttr('selected');
        //OR
        document.getElementById('MyForm').reset();
});

当我再次单击保存"时,不再需要必填字段,并且表单会回发.重置后如何获得重新验证的表单?

When I hit save again, the required fields are no longer required and the form posts back. How can I get the form to re-validate even after a reset?

这是一个简单的示例,因为我只是重置验证和表单,但是在其他页面上,我必须手动操作某些字段,因此我需要这种重置表单的方式来工作.

This is a simple example since I'm just resetting validation and the form, but on other pages I have to manually manipulate certain fields, so I need this way of resetting forms to work.

这是MVC创建的表单,如果有什么不同

This is an MVC created form if that makes any difference

有什么建议吗?

编辑:经过反复试验,看来问题不应该出在直接的resetForm代码上,而应归结于其他问题.

After some trial and error, it seems that the problem shouldn't lie with the direct resetForm code but rather something else.

在控制台中,我收到以下javascript错误:

In the console I get the following javascript error:

SyntaxError: invalid label

和警告

Use of attributes' specified attribute is deprecated. It always returns true.

不确定它们是否与我的问题有关,但不确定当前该怎么做.

Not sure if they're related to my issue but not sure what else to do at this point.

就我正在使用的脚本而言:

As far as the scripts I'm using:

@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/jquery-1.8.3.min.js")
@*@Scripts.Render("~/Scripts/jquery-1.9.1.min.js")*@
@Scripts.Render("~/Scripts/jquery-ui-1.9.2.js")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/jquery-validate-messages.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
@Scripts.Render("~/Scripts/DataTables-1.9.4/media/js/jquery.dataTables.js")
@Scripts.Render("~/Scripts/jquery.jeditable.mini.js")
@Scripts.Render("~/Scripts/DataTables-1.9.4/media/js/jquery.dataTables.editable.js")
@Scripts.Render("~/Scripts/DataTables-1.9.4/extras/TableTools/media/js/TableTools.js")
@Scripts.Render("~/Scripts/Countdown.js")
@Styles.Render("~/Scripts/DataTables-1.9.4/media/css/jquery.dataTables.css")
@RenderSection("scripts", required: false)

1.9.1被注释掉了,因为它在第一次JQuery调用时中断了.这有关系吗?

1.9.1 was commented out because it was breaking on the first JQuery call. Is this related?

推荐答案

找到了一种强制其在提交时重新验证的方法:

Found a way to force it to re-validate on submit:

$(".cancel").click(function () {
        $('#FormId')[0].reset();
        validator.resetForm();
    });

    $("#submitForm").click(function () {
        if ($("#FormId").valid()) {
            $("#FormId").submit();
        }
    });

现在,我不再依赖表单的提交功能来触发验证,而是我自己触发它.

Now I'm no longer relying on the submit feature of the form to trigger validation, but rather I'm triggering it myself.

这篇关于表单重置后,jQuery验证无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:37