使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?


当前回答

# HTML 页面使用 jQuery/JavaScript 方法重定向

尝试此示例代码 :

function YourJavaScriptFunction()
{
    var i = $('#login').val();
    if (i == 'login')
        window.location = "Login.php";
    else
        window.location = "Logout.php";
}

如果您想要给完整 URL 提供完整 URLwindow.location = "www.google.co.in";.

其他回答

使用 jQuery 函数 :

$.extend({
  redirectPost: function(location, args) {
    var form = '';
    $.each(args, function(key, value) {
      form += '<input type="hidden" name="' + key + '" value="' + value + '">';
    });
    $('<form action="' + location + '" method="POST">' + form + '</form>').appendTo($(document.body)).submit();
  }
});

在你的代码中,你使用它这样:

$.redirectPost("addPhotos.php", {pimreference:  $("#pimreference").val(), tag: $("#tag").val()});

使用location.replace()这将调整您的方向, 但不保存上一页的历史。 提交表格时最好使用此选项 。

但当你想保留自己的历史时,你必须使用location.href=//path.

实例:

// Form with steps
document.getElementById('#next').onclick = function() {
   window.location.href='/step2' // Iteration of steps;
}

// Go to next step
document.getElementById('#back').onclick = function() {
   window.history.back();
}

// Finish
document.getElementById('#finish').onclick = function() {
   window.location.href = '/success';
}

// On success page
window.onload = function() {
    setTimeout(function() {
       window.location.replace('/home'); // I can't go back to success page by pressing the back button
    },3000);
}

在 PHP 、 HTML 或 jQuery 区域之后写入下面的代码。如果在 PHP 或 HTML 区域中间,则使用 < shit> 标记。

location.href = "http://google.com"

使用 :

function redirect(a) {
    location = a
}

并称之为:redirect([url]);

没必要href之后location,因为它是隐含的。

您可以在 jQuery 中像这样重定向 :

$(location).attr('href', 'http://yourPage.com/');