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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17748283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:19:43  来源:igfitidea点击:

How to redirect user to another page after Ajax form submission

javascripthtmlajaxformsredirect

提问by June

I'm having problems redirecting the user to a thank you page after a successful form completion. What happens is that after the form submits, it goes to a blank page (https://cunet.sparkroom.com/Sparkroom/postLead)... I need it to redirect to a thank you page while submitting the form details to the URL in the 'form action'.

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

HTML Code:

HTML代码:

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

Ajax Code:

阿贾克斯代码:

<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:

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>

Does anyone know what I'm doing wrong? I need the form to submit the data to the URL and then after redirect the user to a 'thank you' page

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

回答by techfoobar

Your success callback syntax is incorrect. It should rather be:

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

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

Also, note that it is window.location.hrefand not location.window.href

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

回答by edi9999

You should use

你应该使用

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

and not

并不是

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

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

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

The success should be a the functtion passed as an argument to $.ajaxForm:

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

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

回答by edi9999

This is a jQUery Solution:

这是一个 jQUEry 解决方案:

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

回答by Sashant Pardeshi

//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> 

回答by Marco Floriano

The follow code worked for me, in a complete different context of the OP:

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

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

回答by jeetendra Mandal

you can try like this :

你可以这样尝试:

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

回答by verbanicm

Should be

应该

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