javascript - 在 JavaScript 中重定向

标签 javascript html

成功登录后,我尝试将用户重定向到另一个网站。下面的代码不起作用,也不知道为什么。

我用过; window.location.href、document.location.href 和 window.location.replace 也是如此。

这是我的代码:

<!DOCTYPE html>
<html>

<head>

<title></title> 


<link rel="stylesheet" href="login.css" type="text/css">
<script src="login.js">



    </script>

</head>

<body>
<div class="login-page">
  <div class="form">
    <form class="register-form">
      <input type="text" placeholder="name"/>
      <input type="password" placeholder="password"/>
      <input type="text" placeholder="email address"/>
      <button>create</button>
      <p class="message">Already registered? <a href="#">Sign In</a></p>
    </form>
    <form class="login-form">
      <input type="text" placeholder="username" id="username" value="" required/>
      <input type="password" placeholder="password" id="password" value="" required/>
     <button onclick="myFunction()">Login</button>
      <p class="message">Not registered? <a href="#">Create an account</a></p>


    <script>

    function myFunction(){

        var uName= document.getElementById("username").value;
        //alert(uName);
        var pwd= document.getElementById("password").value;
        //alert(pwd);

        var result = loginCheck(uName, pwd);//loginCheck function external 

        //if true             
        if(result==true){

           //re-direct to stack overflow
            window.location.href= "http://stackoverflow.com";

        }

        else{

            alert("you have failed");
        }

    }



    </script>

最佳答案

您的按钮正在尝试提交表单。这可能是一个奇怪的解决方案,但将按钮 type 属性更改为除 submit 之外的任何内容。

例如:

<button type="button" onclick="myFunction()">Login</button>

关于javascript - 在 JavaScript 中重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46697848/

相关文章:

javascript - 如何在 BackboneJS 中显示模型属性的选择列表(下拉列表)?

html - css 中位置变化的过渡

javascript - 在 Safari iOS 上拖放 : Is much of it handled for me or will I have to code most of it

javascript - 仅在页面初始加载时启动 Pace.js

javascript - 从 HTML 创建 JavaScript 数组

jquery - 使用结尾 slider 更改图像

html - 为什么这个 div 不会调整给定位置 :relative; left:0px; right: 200px; 的大小

javascript - 如何为一个元素设置双重属性?

javascript - 如何让按钮出现在列表项旁边?

javascript - 如何居中一个宽的div