我可以获得多个 .submit() 处理程序的集体结果吗?

发布于 2024-12-04 18:00:35 字数 743 浏览 1 评论 0原文

我有一个具有多个 .submit 验证的表单。我想注册一个处理程序,如果集体结果为假,它可以执行某些操作。

这是一个非常简单的片段,说明了我想要做的事情...

$("#myForm").submit(function() {
    if (conditionA)
    {
        return true;
    }
    else {
        return false;
    }
});

$("#myForm").submit(function() {
    if (conditionB)
    {
        return true;
    }
    else {
        return false;
    }
});

    // TODO: Have some event here that knows if Collective result of .submit was true or false
    $("#myForm").submitParentHandler(function (collectiveResult) {
        if (collectiveResult) {
            alert("Form will be submitted");
        }
        else {
            alert("Form will NOT be submitted");
        }
    });

其中最后一个方法目前只是我想要的一个模拟示例。

I have a form that has multiple .submit validations. I would like to register a handler that can do something if the collective result was false.

Here is a very simple snippet that illustrates what I would like to do...

$("#myForm").submit(function() {
    if (conditionA)
    {
        return true;
    }
    else {
        return false;
    }
});

$("#myForm").submit(function() {
    if (conditionB)
    {
        return true;
    }
    else {
        return false;
    }
});

    // TODO: Have some event here that knows if Collective result of .submit was true or false
    $("#myForm").submitParentHandler(function (collectiveResult) {
        if (collectiveResult) {
            alert("Form will be submitted");
        }
        else {
            alert("Form will NOT be submitted");
        }
    });

Where the last method there is currently just an mocked up example of what I would like.

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

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

发布评论

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

评论(1

指尖上得阳光 2024-12-11 18:00:35

我认为你只需要重新构建解决方案。不要注册多个提交处理程序,而是注册包含验证逻辑的方法,如下所示:

 var submitHandlers = [];

 $('#myForm').submit(function()
 {
      var isValid = true;
      $(submitHandlers).each(function()
      {
           isValid = isValid && this();
      });

      return isValid;
 });

然后您只需注册要调用的验证方法:

submitHandlers.push(function()
{
      return conditionA;
});

submitHandlers.push(function()
{
     return conditionB;
});

当您单击提交时,它将运行 submitHandlers 中的所有方法,并且如果它们都返回 true,你将继续。如果有任何返回 false,您将从提交处理程序返回 false。

I think you just need to rearchitect the solution. Instead of registering multiple submit handlers, instead register methods that contain validation logic like so:

 var submitHandlers = [];

 $('#myForm').submit(function()
 {
      var isValid = true;
      $(submitHandlers).each(function()
      {
           isValid = isValid && this();
      });

      return isValid;
 });

Then you would simply register validation methods to invoke:

submitHandlers.push(function()
{
      return conditionA;
});

submitHandlers.push(function()
{
     return conditionB;
});

When you click submit, it would run through all the methods in submitHandlers and if they all returned true, you would continue. If any returned false, you would return false from the submit handler.

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