javascript jQuery/Ajax - 成功消息后重定向到另一个页面

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

jQuery/Ajax - Redirect to another page after succes message

javascriptjqueryhtmlajax

提问by Faizan Ali

I want to redirect the user after filling up the contact form and the success message appears.

我想在填写联系表格并显示成功消息后重定向用户。

Here's the HTML code:

这是 HTML 代码:

<div id="wpm_download_1" style="display: inline;">
   The link to the file(s) has been emailed to you.
</div>

and here's the JavaScript I'm trying:

这是我正在尝试的 JavaScript:

function() { 
      var isDownloaded = jQuery('#wpm_download_1').text(); 
        if (typeof obj.isDownloaded != 'undefined'){
            window.location = 'http://google.com';
        }

    }

Basically, I want to redirect the user when this message appears after sending form data through AJAX:

基本上,我想在通过AJAX以下方式发送表单数据后出现此消息时重定向用户:

The link to the file(s) has been emailed to you..

文件链接已通过电子邮件发送给您。

采纳答案by Faizan Ali

Alright, after trying hard I just came up this and it worked:

好吧,经过努力,我想出了这个,它奏效了:

jQuery( document ).ajaxSuccess(function( event, xhr, settings ) {
  window.location.href = 'http://google.com';
});

Becuase, I wanted to be redirected after the successful AJAX requestis complete.

因为,我想在成功的 AJAX 请求完成后被重定向。

回答by Tit-oOo

Why do you use obj.isDownloaded ? Here it works :

你为什么使用 obj.isDownloaded ?在这里它有效:

 jQuery('#wpm_download_1').text(); 
 if (typeof isDownloaded != 'undefined'){
   window.location = 'http://google.com';
 }

Is up to you to call the function whenever you need it

由您在需要时调用该函数

回答by Wahyu Kodar

Before, you must call your function. And for redirect please which one method below:

之前,您必须调用您的函数。对于重定向,请使用以下哪一种方法:

window.location.href example:

window.location.href 示例:

window.location.href = 'http://www.google.com'; //Will take you to Google.

window.open() example:

window.open() 示例:

window.open('http://www.google.com'); //This will open Google in a new window.