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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17749144/
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-11-01 14:52:39  来源:igfitidea点击:

How to redirect user to another page after Ajax form submission?

javahtmlajaxforms

提问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> 
    $('#theForm').ajaxForm(function() { 
    window.location.href = "I want the user to be redirected to this 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, right now that is not at all happening.

有谁知道我做错了什么?我需要表单将数据提交到 URL,然后在将用户重定向到“谢谢”页面之后,现在根本没有发生。

回答by Igor

Try

尝试

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

回答by Nil'z

Try:

尝试:

$('#theForm').ajaxForm(function() { 
 success : function(){
    window.location.href = "Url to redirect here in the success option";
 }
});

回答by nks

There are two way to redirect user after successful form post.

成功发布表单后,有两种方法可以重定向用户。

1) You can put a on Success event in ajax submission in which you can simply redirect user to thankyou page.

1) 您可以在 ajax 提交中放置一个 on Success 事件,您可以在其中简单地将用户重定向到谢谢页面。

2) Use a server side function after your form post processing done For Ex in PHP:

2) 在 PHP 中完成 For Ex 表单后处理后使用服务器端函数:

header("Location: http://thankyoupage.html");

回答by Zelig880

You could hadle the submit post with jquery, so you will have to cancel the action in the form

您可以使用 jquery 处理提交帖子,因此您必须取消表单中的操作

    <form  name="theForm" id="theForm" >

just give a class or ID to the submit button

只需为提交按钮提供一个类或 ID

    <submit class='submitTheForm'....>

the code will be like this.. you just need to include all the form variables with the right name.

代码将是这样的..您只需要包含具有正确名称的所有表单变量。

Then add the jquery post request at the bottom of MM_validateForm()

然后在MM_validateForm()底部添加jquery post请求

    if ( !jQuery('#theForm #How_Heard').val() ) {
    alert('Please select how you heard about us.');
    jQuery('#theForm #How_Heard').focus();
    return false;
    }

    $.post("https://cunet.sparkroom.com/Sparkroom/postLead/",
{
FirstName:FirstName,
LastName:LastName
    ...
    ...
},
function(){
    window.location.href = 'thank you page'
    }
});

And finally call the function MM_validateForm() when the submit button get clicked

最后在点击提交按钮时调用函数 MM_validateForm()

    $('.submitTheForm').click( function(){
    MM_validateForm();
    });

jquery post from http://api.jquery.com/jQuery.post/the rest from personal experience.

来自http://api.jquery.com/jQuery.post/的jquery 帖子来自个人经验。