javascript - html和javascript中从一个页面到另一个页面的重定向错误?

标签 javascript html

大家好,我有以下代码,我正在尝试重定向到另一个代码,我正在使用 javascript html 来实现此目的,并且我是 html、javascript 和 php 的新手。它显示 test.php,但当我单击按钮时,它不会重定向到 logout_success.php

<!DOCTYPE HTML> 
<html> 
    <head> 
        <title>Sign-In</title> <link rel="stylesheet" type="text/css" href="style-sign.css"> 
    </head>
    <body id="body-color"> 
       <div id="Sign-In"> 
           <fieldset style="width:30%">
               <legend>LOG-IN HERE</legend> 
               <form method="POST" action="connectivity.php"> 
                   User
                   <br>
                   <input type="text" name="user" size="40">
                   <br> 
                   Password 
                   <br>
                   <input type="password" name="pass" size="40"><br> 

                   <input id="button" type="submit" name="submit" value="Log-In" onclick="document.location.href='http://localhost:8080/PortalWork/logout_success.php'"> 
               </form> 
           </fieldset> 
       </div> 
    </body> 
</html> 

最佳答案

有两种方法。

(1)使用AJAX提交表单,并使用window.location.href重定向

$("#form1").submit(function(e){
    e.preventDefault();
    $.ajax({
        url:'connectivity.php',
        method:'POST',
        success:function(rs){
            if( rs == "success" )
               window.location.href("logout_success.php");
        },
        error:function(){
            alert("Error");
        }
    });
    return false;
});

http://jsfiddle.net/hxm49uk2/

(2) 提交到服务器,并使用 header("Location:page.php") 进行重定向;

连接性.php

header("Location:logout_success.php");

关于javascript - html和javascript中从一个页面到另一个页面的重定向错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25663086/

相关文章:

css - 将 div 扩展到页面底部不起作用

javascript - 如何根据按下的按钮填充输入文本字段数据

JavaScript 按钮不工作;代码与其他工作按钮相同?

javascript - 如何从form_dropdown中获取选定的文本?

javascript - 垂直滚动条在 Chrome 中消失( Bootstrap 选项卡)

javascript - pjs 未定义(processingjs)

javascript - 使用 moment-timezone 获取时区的长偏移名称

javascript - 为什么要将 jQuery 代码嵌入到 $(function()) 中?

javascript - 如何等待 jQuery Ajax 请求从 WatiN 完成?

java - 删除 HTML 实体及其内容