MVC3 后不显眼的客户端验证但提交前挂钩可用吗?

发布于 2024-11-15 12:09:02 字数 799 浏览 5 评论 0原文

使用 jquery.validate (1.8.1 ) 库和 ASP MVC3 RTM jquery.validate.ubobtrusive 库,我想知道是否有一个可用的钩子,可以在验证后但在实际表单之前注入一些额外的逻辑已提交。

换句话说:正在提交表单,运行验证(执行通常执行的操作),验证没有发现任何理由阻止表单提交,在此处运行我的代码,然后正常提交。

我知道我可以处理表单的 submit 事件并执行以下操作:

$('#MyForm').submit(function() {
    if (!$(this).valid())
       return false;

    // My Code Here
}

...自己调用验证,然后,如果成功,则输入我自己的逻辑。但我觉得这很脏。我如何知道这是 jquery.validate 的提交处理程序中执行的唯一代码?即使这是唯一正在运行的代码,我真的想复制它吗?我宁愿不必镜像验证库在提交时所做的事情;我只想在验证发生后、表单提交之前注入自己——就像绑定到一个神话般的 $('#MyForm').lastPossibleMomentBeforeSubmit(function() { // My Code Here });< /代码> 事件。

关于“我的代码”应该放在哪里的任何指示?

Using the jquery.validate (1.8.1) library and the ASP MVC3 RTM jquery.validate.ubobtrusive library, I'm wondering if there is a hook available where one could inject some additional logic to occur post-validation, but before the actual form is submitted.

In other words: the form is being submitted, the validation runs (doing whatever it normally does), validation finds no reason to stop the form from submitting, Run My Code Here, then submit as normal.

I know I could handle the form's submit event and do something like:

$('#MyForm').submit(function() {
    if (!$(this).valid())
       return false;

    // My Code Here
}

... calling the validation myself and then, if successful, put in my own logic. But I feel this is dirty. How do I know that this is the only code going on in the jquery.validate's submit handler? Even if this is the only code running, do I really want to duplicate it? I would much rather not have to mirror what the validation library does on submit; I would just like to inject myself after the validation happens, but before the form submits -- like binding to a mythical $('#MyForm').lastPossibleMomentBeforeSubmit(function() { // My Code Here });
event.

Any pointers on where this "My Code Here" should be go?

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

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

发布评论

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

评论(2

你又不是我 2024-11-22 12:09:02

如果您只是使用 jquery.validate 您可以像这样使用成功回调:

$(function () {
    $('#MyForm').validate({
       success : function(error) {
           // additional logic
       }
    });
});

但是,由于您正在使用 jquery.validate.ubobtrusive ,遗憾的是这不能直接完成,因为 jquery.validate.ubobtrusive 隐藏了对 .validate 的调用来自你内部。

相反,您需要获取 jquery.validate.ubobtrusive 传递给 .validate 的设置/选项对象,捕获它的现有成功处理程序,并将其替换为您自己的调用原始处理程序并执行自己的附加功能的对象,如下所示:

$(function () {
            var settings = $('form').data('validator').settings,
                originalSuccess = settings.success;
            settings.success = function (error) {
                originalSuccess(error);
                // additional logic
            };
        });

If you were just using jquery.validate you could use the success call back like this:

$(function () {
    $('#MyForm').validate({
       success : function(error) {
           // additional logic
       }
    });
});

However, because you're using jquery.validate.ubobtrusive sadly this can't be done directly because jquery.validate.ubobtrusive hides the call to .validate from you internally.

Instead you need to get the settings/options object that jquery.validate.ubobtrusive passed to .validate, capture it's existing success handler, and replace it with your own that calls the original and does own additional functionality like this:

$(function () {
            var settings = $('form').data('validator').settings,
                originalSuccess = settings.success;
            settings.success = function (error) {
                originalSuccess(error);
                // additional logic
            };
        });
东京女 2024-11-22 12:09:02

我相信你可以做这样的事情。

$(document).ready(function() {
    $("#MyFormform").validator().bind("onSuccess", function(e, els) {
         //Code to run after validation succeeds
    }
});

I believe you can do something like this.

$(document).ready(function() {
    $("#MyFormform").validator().bind("onSuccess", function(e, els) {
         //Code to run after validation succeeds
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文