【问题标题】:How do you make a submit button redirect to another page after the user has inputted the correct username and password?用户输入正确的用户名和密码后,如何使提交按钮重定向到另一个页面?
【发布时间】:2020-08-14 12:14:34
【问题描述】:

当您输入正确的用户名和密码后单击登录按钮时,它应该会重定向您。我昨天发布了同样的问题,但现在我像一些答案所说的那样稍微更改了代码,但它仍然没有奏效。请帮忙。

<html>
<head>
<title>Login: MessengerX</title>
<link rel="stylesheet" type="text/css" href="C:\Users\Tania\Documents\Website\style.css">
<meta charset="UTF-8">
<meta name="description" content="HTML website called MessengerX. Send messages to anyone who has logged in.">
<meta name="author" content="Joshua Hurley">
<meta name="keywords" content="HTML, MessengerX, Joshua Hurley, Website, Supremefilms">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="css/style.css"/>


</head>
<body>
  <div class="imgcontainer">
    <img src="C:\Users\Tania\Documents\Website\Screenshots\face.jpg" height="500" alt="Avatar" class="avatar">
  </div>
<div class="container">
<div class="main">
<h1 style="color:"><b><i>Login: MessengerX</i></b></h1>
<form id="form_id" method="post" name="myform">
<label>User Name :</label>
<input type="text" name="username" id="username"/>
<label>Password :</label>
<input type="password" name="password" id="password"/>
 <button type="submit" value="Login" id="submit" onclick="validate()"/>
</form>
<b><i>Submit</i></b>
</div>
</div>
<script type="text/javascript">
   window.addEventListener('load', (event) => {
     var attempt = 3;
     // Below function Executes on click of login button.
     function validate(){
     var username = document.getElementById("username").value;
     var password = document.getElementById("password").value;
     if ( username == "Joshua" && password == "Joshua#123"){
     alert ("Login successfully");
      window.location.replace("https://www.youtube.com"); // Redirecting to other page.
     }
     else{
     attempt -= 1;// Decrementing by one.
     alert("Incorrect username or password")
     alert("You have "+attempt+" attempt(s) left;");
     // Disabling fields after 3 attempts.
     if( attempt == 0){
       alert("Incorrect username or password")
     document.getElementById("username").disabled = true;
     document.getElementById("password").disabled = true;
     document.getElementById("submit").disabled = true;
     return false;
     }
     }
     }
 });
</script>
<script src="js/login.js"></script>
</body>
</html>

【问题讨论】:

  • javascript 中的用户验证?没有任何API?严重地?它很容易破解,你可能根本不做任何验证,几乎一样
  • 这能回答你的问题吗? How do I redirect to another webpage?
  • 你能说得更具体一些吗?重定向?登录代码?有什么错误?任何警报被触发?如果是,是哪一个?
  • 表单提交已经重定向(到同一页面),当有待提交的表单时,您不能从JS重定向。

标签: javascript html authentication redirect


【解决方案1】:

你必须阻止提交的默认行为,一旦你这样做了 - 你可以重定向。

    $("form").submit(function(event){ //edited
        event.preventDefault()
..your code here
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-18
    • 2013-07-18
    相关资源
    最近更新 更多