使用 JS 从 HTML 按钮重定向到新页面

问题描述 投票:0回答:2

这相当简单,我正在尝试使登录按钮正常工作,如果电子邮件和密码中的内容与以下内容匹配,则重定向到不同的文件(placeholder.html):

登录:管理员 密码:密码123

我还没有让登录按钮首先进行重定向,并且 id 喜欢通过使用 'onlick=' 和 js 中的函数或 id 来将其保留为按钮元素。

非常感谢您的帮助:) 抱歉,如果这有点草率,这是我第一次在这里寻求帮助哈哈

这是我到目前为止所拥有的,它完全有效:

<!DOCTYPE html>
<html>

  <head>
    <title>Login page</title>
    <link href="style." rel="stylesheet" type="text/css" />
    <script src="script.js"></script>
  </head>

  <body>
    
    <div class="login-page">
    
      <div name="login" class="form">

        <div class="title login" style="font-size: 25px; font-family: Roboto; padding-bottom: 25px;">
          Login Form
        </div>

        <form class="login-form" action="login.php" method="post">

          <input type="email_" placeholder="email"/>

          <input type="password" placeholder="password"/>

          <button id="login_button">login</button>

          <p class="message" style="font-family: Roboto, sans-serif;">Not registered? 
            <a href="register.html">Create an account</a>
          </p>

        </form>

      </div>

    </div>

  </body>

  <script type="text/javascript">

    document.getElementById("login_button").onclick = function () {
        location.href = "#placeholder";
    };

</script>

</html>
javascript html authentication
2个回答
2
投票

该函数正在 onClick 上运行,但您使用

location.href
的方式是无效的。您正在重定向到一个锚点(它以
#
开头),在浏览器中实现这一点的方式是,如果页面上有一个带有 ID 的元素,它会将您的滚动定位重定向到该元素的顶部
placeholder
的ID(并且页面高度超过100%)它会移动页面的滚动位置,而不是重定向。

我相信你正在寻找

location.href = "placeholder.html";

您希望用户根据输入的用户名和密码进行重定向,我将提供下面的解决方案,但在此之前,我想说这不安全,用户名和密码在源代码中可见,并且如果您打算使用它来隐藏重要信息,它将不起作用,因为访问该网页的任何人都可以看到该代码,您应该使用服务器端语言(例如 NodeJS 或 PHP)来隐藏该信息,我认为这是只是为了练习/学习。

您要做的第一件事就是获取用户名和密码。

您的

<input type="email_" placeholder="email"/>
应该是
<input type="text" placeholder="email"/>
。由于某种原因,您将类型设置为看起来是拼写错误的
email_
而不是
email
,但既然您说用户名是
admin
,那就只是
text

我首先为用户名和密码元素提供一个 ID。

<input type="email" id="username" placeholder="email"/>
<input type="password" id="password" placeholder="password"/>

然后在你的函数中,你可以将其更改为检查和重定向,如下所示

document.getElementById("login_button").onclick = function () {
  const username = document.getElementById("username").value
  const password = document.getElementById("password").value

  if (username === 'Admin' && password === 'Password123') {
    location.href = "placeholder.html";
  }
};

这也区分大小写。


0
投票

有几种方法可以做到这一点,这里是一个简单的例子:

<!DOCTYPE html>
<html>

  <head>
    <title>Login page</title>
    <link href="style." rel="stylesheet" type="text/css" />
    <script src="script.js"></script>
  </head>

  <body>
    
    <div class="login-page">
    
      <div name="login" class="form">

        <div class="title login" style="font-size: 25px; font-family: Roboto; padding-bottom: 25px;">
          Login Form
        </div>

        <form class="login-form" action="login.php" method="post">

          <input type="email_" placeholder="email"/>

          <input type="password" placeholder="password"/>

          <button id="login_button" onclick="login()">login</button>

          <p class="message" style="font-family: Roboto, sans-serif;">Not registered? 
            <a href="register.html">Create an account</a>
          </p>

        </form>

      </div>

    </div>

  </body>

  <script type="text/javascript">

    function login() {
      // Login logic here
      if (loginSuccesful) {
        window.location.href = "/placeholder.html";
      }
    }

</script>

</html>
© www.soinside.com 2019 - 2024. All rights reserved.