使用对话框进行 jQuery 验证

发布于 2024-09-29 08:17:28 字数 3436 浏览 0 评论 0原文

我使用错误消息对话框使 jQuery 验证工作得很好。我遇到的问题是,当我单击“提交”并且所有字段都已正确填写时,在表单提交之前会弹出一个空白对话框。我似乎不知道如何阻止这种情况发生。将 onSubmit 设置为 false 将不起作用,因为它根本不会验证。有什么想法吗?

这是包含我所有 jQuery 的脚本标签:

    <script type="text/javascript">
    $(document).ready(function () {
        $("#tabs").tabs();

        $("#DateOfBirth").datepicker({ changeYear: true, changeMonth: true });

        $("#InquiryForm").validate({
            submitHandler: function (form) {
                //form.submit();
            },
            rules: {
                FirstName: { required: true },
                LastName: { required: true },
                Phonenumber: { required: true },
                Email: { required: true, email: true },
                Address: { required: true },
                Country: { required: true },
                Zip: { required: true },
                CourseDeliveryTime: { required: true },
                ProgramType: { required: true },
                ProgramofInterest: { required: true },
                ProgramType: { required: true },
                DateOfBirth: { required: true, date: true }
            },
            messages: {
                FirstName: { required: 'First Name is required.<br/>' },
                LastName: { required: 'Last Name is required.<br/>' },
                Phonenumber: { required: 'Phone Number is required.<br/>' },
                Email: { required: 'E-Mail is required.<br/>', email: 'Please enter a valid e-mail address' },
                Address: { required: 'Mailing Address is required.<br/>' },
                Country: { required: 'Country is required.<br/>' },
                Zip: { required: 'Zip Code is required.<br/>' },
                CourseDeliveryTime: { required: 'Please tell us when you would like to attend class.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                ProgramofInterest: { required: 'Intended Academic Program is required.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                DateOfBirth: { required: 'Date of Birth is required.', date: 'Please enter a valid date i.e. 01/01/1999' }
            },
            errorPlacement: function (error, element) {
                error.appendTo($("#dialog"));

            },
            showErrors: function (errorMap, errorList) {
                this.defaultShowErrors();
                $("#dialog").dialog('open');
            },
            errorContainer: "#dialog",
            errorLabelContainer: "#dialog ul",
            wrapper: "li", debug: true,
            onfocusout: false,
            onclick: false
        });
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
            close: function (event, ui) {
                $("#dialog").html("");
            },
            title: 'Please fix the following:',
            resizable: false,
            width: 300
        });
        $("#privacyPolicy").dialog({
            autoOpen: false,
            modal: true,
            title: 'Privacy Policy',
            resizable: false,
            height: 210,
            width: 500
        });
        $("#linkPrivacyPolicy").click(function () {
            $("#privacyPolicy").dialog("open");
            return false;
        });
    });
</script>

I have jQuery validation working pretty well using a dialog for the error messages. The problem I am having is that when I click submit, and all the fields are filled out correctly, I have a blank dialog popping up before the form submits. I can't seem to figure out how to stop that from happening. Setting the onSubmit to false will not work because then it won't validate at all. Any ideas?

This is the script tag with all my jQuery in it:

    <script type="text/javascript">
    $(document).ready(function () {
        $("#tabs").tabs();

        $("#DateOfBirth").datepicker({ changeYear: true, changeMonth: true });

        $("#InquiryForm").validate({
            submitHandler: function (form) {
                //form.submit();
            },
            rules: {
                FirstName: { required: true },
                LastName: { required: true },
                Phonenumber: { required: true },
                Email: { required: true, email: true },
                Address: { required: true },
                Country: { required: true },
                Zip: { required: true },
                CourseDeliveryTime: { required: true },
                ProgramType: { required: true },
                ProgramofInterest: { required: true },
                ProgramType: { required: true },
                DateOfBirth: { required: true, date: true }
            },
            messages: {
                FirstName: { required: 'First Name is required.<br/>' },
                LastName: { required: 'Last Name is required.<br/>' },
                Phonenumber: { required: 'Phone Number is required.<br/>' },
                Email: { required: 'E-Mail is required.<br/>', email: 'Please enter a valid e-mail address' },
                Address: { required: 'Mailing Address is required.<br/>' },
                Country: { required: 'Country is required.<br/>' },
                Zip: { required: 'Zip Code is required.<br/>' },
                CourseDeliveryTime: { required: 'Please tell us when you would like to attend class.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                ProgramofInterest: { required: 'Intended Academic Program is required.<br/>' },
                ProgramType: { required: 'Preferred Location is required.<br/>' },
                DateOfBirth: { required: 'Date of Birth is required.', date: 'Please enter a valid date i.e. 01/01/1999' }
            },
            errorPlacement: function (error, element) {
                error.appendTo($("#dialog"));

            },
            showErrors: function (errorMap, errorList) {
                this.defaultShowErrors();
                $("#dialog").dialog('open');
            },
            errorContainer: "#dialog",
            errorLabelContainer: "#dialog ul",
            wrapper: "li", debug: true,
            onfocusout: false,
            onclick: false
        });
        $("#dialog").dialog({
            autoOpen: false,
            modal: true,
            close: function (event, ui) {
                $("#dialog").html("");
            },
            title: 'Please fix the following:',
            resizable: false,
            width: 300
        });
        $("#privacyPolicy").dialog({
            autoOpen: false,
            modal: true,
            title: 'Privacy Policy',
            resizable: false,
            height: 210,
            width: 500
        });
        $("#linkPrivacyPolicy").click(function () {
            $("#privacyPolicy").dialog("open");
            return false;
        });
    });
</script>

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

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

发布评论

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

评论(1

贱贱哒 2024-10-06 08:17:28

您可以将开放代码移至 invalidHandler ,这样它仅在出现错误时运行,方法是将以下内容替换

showErrors: function (errorMap, errorList) {
  this.defaultShowErrors();
  $("#dialog").dialog('open');
},

为:

invalidHandler: function() {
  $("#dialog").dialog('open');
},

You can move your open code to the invalidHandler so it only runs when there are errors, by replacing this:

showErrors: function (errorMap, errorList) {
  this.defaultShowErrors();
  $("#dialog").dialog('open');
},

With this:

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