在JavaScript数组中存储Html输入

问题描述 投票:-2回答:1

我打算创建一个示例登录页面。我们必须在注册表中从用户那里获取基本信息,并将所有细节存储在Js数组中。

并且用户需要以登录形式输入他们的名称和密码,在页面后面我们需要检查登录表单中输入的详细信息以及存储在js数组中的数据。

这是我的尝试:

<!DOCTYPE html>
<html>
<head>
    <script type="text/JavaScript">
        var users = [];

        function register() {
            users["name"] = [];
            users["password"];
            usr = document.getElementById('usr');
            pwd = document.getElementById('pwd');
            users["name"].push(use);
            users["password"].push(pwd);
        }

        function login() {
            uname = document.getElementById('uname');
            passwd = document.getElementById('passwd');

            if (uname == users["name"]) {
                if (passwd == users["password"]) {
                    alert("Welcome!")

                } else {
                    alert("Wrong Password!");
                }
            } else {
                alert("Wrong Username!");
            }
        }

    </script>
    <title> Portal </title>
</head>
<body>
    <p>
    <h3>Register</h3><br>
    Username:
    <input id="usr" type="text"><br>
    Password:
    <input id="pwd" type="password">
    <br>
    <input type="button" onclick="register">
    <br>
    <h3>Login</h3><br>
    Username:
    <input id="uname" type="text"><br>
    Password:
    <input id="passwd" type="text"><br>
    <input type="button" onclick="login()">
    </p>
</body>
</html>
javascript html arrays input
1个回答
0
投票

你忘了onclick=register的括号+如果你像这样初始化users数组会更好

<!DOCTYPE html>
<html>
<head>
<script type="text/JavaScript">
var users={name:[], password:[]};
function register ()
{
usr=document.getElementById('usr');
pwd=document.getElementById('pwd');
users["name"].push(use);
users["password"].push(pwd);
}

function login ()
{
uname=document.getElementById('uname');
passwd=document.getElementById('passwd');

if(uname==users["name"])
{
if(passwd==users["password"])
{
alert("Welcome!")!
}
else
{
alert("Wrong Password!");
}
else
{
alert("Wrong Username!");
}

</script>
<title> Portal </title>
</head>
<body>
<p><h3>Register</h3><br>
Username:
<input id="usr" type="text"><br>
Password:
<input id="pwd" type="password">
<br>
<input type="button" onclick="register()">
<br>
<h3>Login</h3><br>
Username:
<input id="uname" type="text"><br>
Password:
<input id="passwd" type="text"><br>
<input type="button" onclick="login()">
</p>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.