如何在表单提交后使用jQuery验证重定向

时间:2014-05-27 18:49:20

标签: jquery jquery-plugins

我正在使用jQuery validate插件来提交联系表单。

问题是我应该重定向到“谢谢你”。成功提交表单后的页面。

这可能吗?

我的验证码如下所示:

$("#contactForm").validate({
  //debug: true,

  rules: {

    fullname: "required",

    email: {
      required: true,
      email: true
    }
  },
  submitHandler: function(form) {
    form.submit();
    window.location.href = "/confirm.html";
  }

});

出于某种原因

window.location.href =" /confirm.html");

永远不会被执行。

感谢您的帮助,

安东尼

1 个答案:

答案 0 :(得分:0)

经过一些试验和错误后,我设法重定向到了“谢谢你”'将vaild表单提交到Simple Form后的页面。这必须通过ajax调用来完成:

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js"></script>
<script>
$("#contactForm").validate({
rules: {
    fullname: "required",
    email: {
      required: true,
      email: true
    }
  },

  submitHandler: function(form) {
    $.ajax({
      dataType: 'jsonp',
      url: "http://getsimpleform.com/messages/ajax?form_api_token=my-secret-token",
          data: $("#contactForm").serialize()
        }).done(function() {
      //callback which can be used to show a thank you message
      //and reset the form
      window.location.href = "/confirm.html";
    });
  }
});
</script>

不,我无法验证联系表单并将其提交给Simple Form服务,然后重定向到“谢谢”&#39;页面,所有没有任何服务器端代码:微笑:

相关问题