如何在Ajax表单提交后将用户重定向到另一个页面

时间:2013-07-19 14:13:08

标签: javascript html ajax forms redirect

我在成功完成表单后将用户重定向到感谢页面时遇到问题。结果是,在表单提交后,它会转到空白页面(https://cunet.sparkroom.com/Sparkroom/postLead)...我需要将其重定向到感谢页面,同时将表单详细信息提交到“表单操作”中的URL。

HTML代码:

<form action="https://cunet.sparkroom.com/Sparkroom/postLead/" method="post" name="theForm" 
id="theForm" onSubmit="return MM_validateForm();" >
...
</form>

Ajax代码:

<script src="http://malsup.github.com/jquery.form.js"></script> 
<script> 
    $(document).ready(function() { 
        $('#theForm').ajaxForm(function() { 
           alert('form was submitted');
        }); 
     success:function(response) {
           location.window.href = "redirect user to the thank you page";
                }
    }); 
</script> 

JavaScript:

function MM_validateForm() {
if ( !jQuery('#theForm #FirstName').val() ) {
alert('Please input your first name.');
jQuery('#theForm #FirstName').focus();
return false;
}
if ( !jQuery('#theForm #LastName').val() ) {
alert('Please input your last name.');
jQuery('#theForm #LastName').focus();
return false;
}
if ( !jQuery('#theForm #daytimephone').val() ) {
alert('Please input your phone number.');
jQuery('#theForm #daytimephone').focus();
return false;
}
if ( !jQuery('#theForm #Email').val() ) {
alert('Please input your email.');
jQuery('#theForm #Email').focus();
return false;
}
if ( !jQuery('#theForm #BID').val() ) {
alert('Please select your preferred campus.');
jQuery('#theForm #BID').focus();
return false;
}
if ( !jQuery('#theForm #programs').val() ) {
alert('Please select your preferred program.');
jQuery('#theForm #programs').focus();
return false;
}
if ( !jQuery('#theForm #How_Heard').val() ) {
alert('Please select how you heard about us.');
jQuery('#theForm #How_Heard').focus();
return false;
}
return true;
}
// ]]></script>

有谁知道我做错了什么?我需要表单将数据提交到URL,然后将用户重定向到“谢谢”页面

7 个答案:

答案 0 :(得分:14)

您的成功回调语法不正确。它应该是:

$('#theForm').ajaxForm(function() { 
    window.location.href = "/path/to/thankyoupage";
});

另请注意,它是window.location.href而不是location.window.href

答案 1 :(得分:1)

这是一个jquery解决方案:

window.location("www.example.com");

答案 2 :(得分:1)

//Please try this    
<script src="http://malsup.github.com/jquery.form.js"></script> 
    <script> 
        $(document).ready(function() { 
            $('#theForm').ajaxForm(function() { 
               alert('form was submitted');
            }); 
         success:function(response) {
               if(response){ // check whether response is received
                    location.window.href = "http://your_domain_name/thank-you";}
               }
        }); 
    </script> 

答案 3 :(得分:0)

你应该使用

window.location.href = "http://www.google.com";

而不是

location.window.href = "http://www.google.com";

http://www.w3schools.com/js/js_window_location.asp

成功应该是作为$ .ajaxForm的参数传递的功能:

$('#theForm').ajaxForm(function() { 
           window.location.href = "redirect user to the thank you page";
        }); 

答案 4 :(得分:0)

在OP的完全不同的上下文中,以下代码对我有用:

$(document).ready(function() {
  window.location.href = "URL";
  });

答案 5 :(得分:0)

您可以尝试这样:

 success: function (data) {
    window.location.href = data.redirecturl;
},
error: function () {
    alert('error happened');
}

答案 6 :(得分:-2)

应该是

window.location = "http://www.google.com";