简体   繁体   English

jQuery提交表单后如何重定向

[英]how to redirect after form submission with jQuery validate

I am using the jQuery validate plugin to valide a contact form before it is submitted. 我正在使用jQuery validate插件在提交联系表单之前对其进行验证。

The problem is that I should redirect to a 'thank you' page after a successful form submission. 问题是成功提交表单后,我应该重定向到“谢谢”页面。

Is this possible? 这可能吗?

My validation code looks like this: 我的验证代码如下所示:

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

  rules: {

    fullname: "required",

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

});

for some reason 由于某些原因

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

is never executed. 永远不会执行。

Thanks for your help, 谢谢你的帮助,

Anthony 安东尼

After some trial and error I managed to redirect to a 'thank you' page after a vaild form submission to Simple Form. 经过一番尝试和错误之后,在将表单提交到简单表单后,我设法重定向到“谢谢”页面。 This has to be done trough an ajax call: 这必须通过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>

No I'm able to validate a contact form and submit it to the Simple Form service, and then redirect to a 'thank you' page, all without any server side code :smile: 不,我能够验证联系表单并将其提交给简单表单服务,然后重定向到“谢谢”页面,所有这些都无需任何服务器端代码:smile:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM