jQuery - 为什么 ajaxForm 绕过验证过程?

发布于 2024-09-13 05:49:12 字数 847 浏览 4 评论 0原文

我有以下代码片段:

$(document).ready(function () {
      // bind 'regFormBody' and provide a simple callback function
      $('#regFormBody').ajaxForm(function() { 
          alert("Thank you for your comment!"); 
      }); 

      // validate the #regFormBody form when it is submitted
      $("#regFormBody").validate({
        submitHandler: function(form) { 
          alert('form is submitted');
        },        
        rules: {
          ...
        },
        messages: {
          ...
        }
      });
}      

问题是在我添加

    // bind 'regFormBody' and provide a simple callback function
    $('#regFormBody').ajaxForm(function() { 
        alert("Thank you for your comment!"); 
    }); 

表单验证之后根本不起作用。即使没有在表单中输入任何信息,我总是会看到消息警报(“表单已提交”)。

你能告诉我如何解决这个问题吗?

谢谢

I have the following code snippet:

$(document).ready(function () {
      // bind 'regFormBody' and provide a simple callback function
      $('#regFormBody').ajaxForm(function() { 
          alert("Thank you for your comment!"); 
      }); 

      // validate the #regFormBody form when it is submitted
      $("#regFormBody").validate({
        submitHandler: function(form) { 
          alert('form is submitted');
        },        
        rules: {
          ...
        },
        messages: {
          ...
        }
      });
}      

The problem is that after I add the

    // bind 'regFormBody' and provide a simple callback function
    $('#regFormBody').ajaxForm(function() { 
        alert("Thank you for your comment!"); 
    }); 

The form validation doesn't work at all. I always see the message alert('form is submitted') even without entering any information to form.

May you tell me how to solve this problem?

Thank you

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

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

发布评论

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

评论(1

心凉怎暖 2024-09-20 05:49:12

您可以扩展 .ajaxForm() 的选项对象,像这样:

$('#regFormBody').ajaxForm({
  beforeSubmit: function() {
    return $('#regFormBody').valid();
  },
  success: function() { 
    alert('Thanks for your comment!'); 
  } 
});

这将在提交之前启动验证,如果不是 .valid () 它会阻止提交按照您想要的方式发生。

You can expand your options object for .ajaxForm(), like this:

$('#regFormBody').ajaxForm({
  beforeSubmit: function() {
    return $('#regFormBody').valid();
  },
  success: function() { 
    alert('Thanks for your comment!'); 
  } 
});

This will kick off validation before submitting, and if it's not .valid() it'll stop the submit from happening like you want.

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