Jquery Unobtrusive 验证在实现 onSubmit 事件后丢失

发布于 2024-12-06 19:41:56 字数 1304 浏览 1 评论 0原文

我正在使用 ASP.NET MVC。在我将事件附加到表单的 onSubmit 事件之前,我的验证工作正常。

这就是我的表单的外观:

@using (Html.BeginForm("Recover", "Auth", FormMethod.Post, new { Id = "RecForm", onSubmit = "return Events.Submit(this,event)" }))
{
        @Html.TextBoxFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
        <input type="submit" value="Recover" />
}

Javascript(工作正常,只需将其放在这里,以便您可以检查其中是否有某些内容导致验证不触发):

//Events
window.Events = (function () {

    // Baseline setup
    var Events = {};

    function disableAllChildren(el) {
        $(el).find(':input').attr('disabled', true);
    }
    function enableAllChildren(el) {
        $(el).find(':input').attr('disabled', false);
    }

    //Ajax submit
    Events.Submit = function (el, event) {
        event.preventDefault();
        //serialize the form before disabling the elements.
        var data = $(el).serialize();

        disableAllChildren(el);
        $(el).find('.success, .error').hide();
        $.ajax({
            url: el.action,
            type: 'POST',
            data: data,
            success: function (data) {
                //Stuff happens.
            }
        });
    };
    return Events;
})(this, this.document);

如何取回我的验证?

I'm using ASP.NET MVC. My validation works well until I attached an event to the form's onSubmit event.

This is how my form looks:

@using (Html.BeginForm("Recover", "Auth", FormMethod.Post, new { Id = "RecForm", onSubmit = "return Events.Submit(this,event)" }))
{
        @Html.TextBoxFor(model => model.Email)
        @Html.ValidationMessageFor(model => model.Email)
        <input type="submit" value="Recover" />
}

The Javascript (works fine, just put it here so you can check if something in this is causing the validation not to fire):

//Events
window.Events = (function () {

    // Baseline setup
    var Events = {};

    function disableAllChildren(el) {
        $(el).find(':input').attr('disabled', true);
    }
    function enableAllChildren(el) {
        $(el).find(':input').attr('disabled', false);
    }

    //Ajax submit
    Events.Submit = function (el, event) {
        event.preventDefault();
        //serialize the form before disabling the elements.
        var data = $(el).serialize();

        disableAllChildren(el);
        $(el).find('.success, .error').hide();
        $.ajax({
            url: el.action,
            type: 'POST',
            data: data,
            success: function (data) {
                //Stuff happens.
            }
        });
    };
    return Events;
})(this, this.document);

How do I get back my validations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

☆獨立☆ 2024-12-13 19:41:56

我所做的就是在提交之前检查表格是否有效。请参阅下面的代码:

$form = $("#myForm");

if (form.valid()) {

    form.validate();
    //other codes here

 }

如果表单有效,则发布您的数据。如果没有,则什么都不做。

What I do is to check if the form is valid before I submit it. See the below code :

$form = $("#myForm");

if (form.valid()) {

    form.validate();
    //other codes here

 }

If the form is valid, then post your data. If not, do nothing.

贱贱哒 2024-12-13 19:41:56

您的代码中存在比验证更多的问题。情况是 disabled 字段不会发布到服务器。他们也不参与验证。因此,即使您的代码有效,您也不会收到任何内容发布到服务器端。删除 disableAllChildren(el); 或将其重新编码为不禁用字段,但可能使它们只读。

这样,它们就会得到验证,但在 ajax 提交之后也就不足为奇了。这是您不希望出现的行为,因此请查看验证方法。尤其是你应该对它的部分感兴趣

有效时通过 Ajax 提交表单。

我相信通过 ajax 验证表单提交可以更轻松地完成。

$(".selector").validate({
   submitHandler: function(form) {
    $(form).ajaxSubmit();
   }
})

或者,只需在 ajax 提交之前添加验证检查

There're more problems in your code than validation. Case is that disabled fields do not get posted to server. They do not participate in validation either. So, even if your code worked, you would get nothing posted to server side. Remove disableAllChildren(el); or recode it to not disable fields, but maybe make them readonly.

With this, they will get validated, but not surprisingly after the ajax submit. This is behavior you should not wish, so take a look at validate method. Especially you should be interested in part where it

Submits the form via Ajax when valid.

I believe that way validated form ajax submission can be done easier.

$(".selector").validate({
   submitHandler: function(form) {
    $(form).ajaxSubmit();
   }
})

Or, just add validation checking right before ajax submit

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文