【问题标题】:JQuery Validator - Success function not being called on valid submissionJQuery Validator - 有效提交时未调用成功函数
【发布时间】:2013-05-21 09:54:32
【问题描述】:

我正在使用来自:http://bassistance.de/jquery-plugins/jquery-plugin-validation/ 的 JQuery 验证器

我已经编写了我需要的 JQuery,它可以正确验证,但是我的提交函数的最后一部分无法正常工作。

$("#commentForm").validate({
    rules: {
        email_options_name: {
            required: true
        },
        email_options_company: {
            required: true
        },
        email_options_country: {
            required: true
        },
        email_option_email: {
            required: true,
            email: true
        },
        check_options: {
            required: true,
            minlength: 1
        }
    },      
});

$("#commentForm").submit(function(){

    var valid = $("#commentForm").valid();
    if (valid) {
                $.post('scripts/process_email_initial_form.php',$(this).serialize(), function(o) {
                    location.reload();
                }, 'json');
            } else {
                $('.full-error').show();
            }

                return false;
});

$.extend($.validator.messages, {
            required: "!",
            email: "!"
});

}

当提交有效时,脚本process_email_initial_form.php 被调用并正确处理(通过查看firebug),如果不是,则显示div。问题是当脚本完成运行时location.reload(); 不会调用。理想情况下,我想指向成功页面,但这也不起作用。

我尝试在process_email_initial_form.php 的末尾添加一个 php 标头函数,但这也不起作用。

非常感谢任何帮助。

【问题讨论】:

  • Read the documentation使用submitHandler 回调函数。 这是您放置ajax 的位置。您的外部 submit 处理程序正在搞砸插件的内置提交处理程序。

标签: php javascript jquery validation jquery-validate


【解决方案1】:

根据docs,submitHandler 回调是“验证后通过 Ajax 提交表单的正确位置。”

$("#commentForm").validate({
    rules: {
        // your rules here
    },
    submitHandler: function(form) {
       // example below uses the 'form' plugin
       // http://www.malsup.com/jquery/form/#api
       $(form).ajaxSubmit({
            success: function(){
                window.location.href="newPage.php"
            }
       });
    }      
});

【讨论】:

    【解决方案2】:

    您只检查了表单是否有效。通过检查这将仅返回布尔值,它不会设置错误消息。您需要在显示之前将错误分配给 $('.full-error')。

    试试

    $("#commentForm").validate();
    

    【讨论】:

      【解决方案3】:

      您尝试过使用done 事件吗?

      $.post(
          'scripts/process_email_initial_form.php',
          $(this).serialize(), 
          function(o) {
             console.log('success');
          },
          'json'
      ).done(function(o){
          window.location.replace("success.php");
      });
      

      ajax 页面中的 PHP 标头不会将当前页面重定向到成功页面。它只会使您的 ajax 页面重定向到定义的标题。

      您可以尝试使用 jquery 提交表单,而不是使用 window.location.replace

      $("#commentForm").submit(function(){
          var valid = $("#commentForm").valid();
          if (valid) {
              $.post(
              'scripts/process_email_initial_form.php',
              $(this).serialize(),
              function(o) {
                  console.log('success');
              },
              'json')
              .done(function(o) {
                  $(this).attr('action', 'success.php');
                  return true;
              });
          } else {
              $('.full-error').show();
              return false;
          }
      });
      

      $("#commentForm").submit(function(){
          var valid = $("#commentForm").valid();
          if (valid) {
              $.post(
              'scripts/process_email_initial_form.php',
              $(this).serialize(),
              function(o) {
                  $(this).attr('action', 'success.php');
                  return true;
              },
              'json');
          } else {
              $('.full-error').show();
              return false;
          }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-14
        • 2023-03-30
        • 2012-08-14
        • 1970-01-01
        • 2015-01-15
        • 1970-01-01
        相关资源
        最近更新 更多