弹出登录页面

时间:2020-04-26 04:33:04

标签: javascript html

我已经编写了一些代码来使用JavaScript语言(没有数据库)在索引页面中构建弹出式登录名。登录页面设置有恒定的用户名和密码,例如,在这种情况下,用户名= xxx,密码= nnn。

下面是我在索引页面和登录弹出页面上的代码:

索引页代码:

    <!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/timetabling.css">
        <title>Time-tabling system</title>
    </head>
    <body>

        <div id="main">
            <div id="header">
                <div id="widgetBar">
                    <div class="logo">
                        <img align="left" src="logo.jpg" alt="logo" width="300" height="100">
                    </div>
                    <div class="button">
                        <button class="btn login" id="login_button">Login</button>

                        <div class="popup">
                            <div class="popup-content">
                                <img scr="close.png" alt="Close" class="close">
                                <input type="text" placeholder="username">
                                <input type="password" placeholder="password">
                                <button>login</button>
                            </div>
                        </div>
                        <script language="javascript" src="login.js"></script>
                    </div>
                </div>
            </div>

            <img align="center" src="keep-habbit-tracking.jpg" alt="habbit" width="600" height="300">
            <p align="center">Welcome to the time-tabling system!!</p>
            <p align="center">This is a place for you to set your time table and schedule your work...</p>
            <p align="center">Please login to begin</p>

            <div id="footer">
                <hr>
                <p id="footerText">Group 18 The time tabling system</p>
            </div>
        </div>
    </body>
</html>

弹出窗口登录页面的代码:

  document.getElementById("login_button").addEventListener("click", function(){
        document.querySelector(".popup").style.display = "flex";
        })

        document.querySelector(".close").addEventListener("click", function(){
        document.querySelector(".popup").style.diplay = "none";
        })


        var attempt = 3; //Variable to count number of attempts
        function validation(){
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            if(username === "xxx" && password === "nnn"){
                alert("Login successfully");
                window.location="swap.html";
                return false;
            }
            else{
                attempt--;
                alert("You have left "+attempt+" attempt;");
                if(attempt ===0){
                    document.getElementById("username").disabled=true;
                    document.getElementById("password").disabled=true;
                    document.getElementById("submit").disabled=true;
                    return false;
                }
            }
        }

我可以显示一个弹出页面供用户使用这些代码登录,但是我无法检查用户名和密码是否为xxx和nnn。另外,我的弹出式登录页面中的登录按钮单击后无法将我定向到另一个页面。

任何人都可以帮忙,我逐行阅读代码,没有发现任何问题。谢谢![enter image description here] 1

1 个答案:

答案 0 :(得分:0)

我认为您忘记在文本字段中提供id属性

                     <div class="popup-content">
                        <img scr="close.png" alt="Close" class="close">
                        <input id="username" type="text" placeholder="username">
                        <input id="password" type="password" placeholder="password">
                            <button  onclick="validation()">login</button>
                    </div>