我一直致力于一项任务,并且我被要求通过实现哈希映射使用HTML和javascripts创建一个登录页面,我已经使用变量实现了它。 cuser和cpass拥有正确的用户名和密码以及" t1"和" t2"是用户输入用户名和密码的文本字段。
var cuser="admin";
var cpass="pass";
var user=document.getElementById("t1").value;
var pass=document.getElementById("t2").value;
我使用了if条件来验证用户名和密码
if(user == cuser && pass == cpass)
{
alert("You are logged in as : " +cuser);
}
我必须使用hashmaps实现这一点,有人可以帮我解决这个问题,我如何创建哈希表并使用密钥和值验证用户和密码。
答案 0 :(得分:1)
以这种方式尝试,
var credentials = {cuser:"admin",cpass:"pass"};
var user=document.getElementById("t1").value;
var pass=document.getElementById("t2").value;
if(user == credentials['cuser'] && pass == credentials['cpass']) {
alert("You are logged in as : " +cuser);
}
或者您可以使用点表示法而不是像credentials.cuser
答案 1 :(得分:0)
我会这样做的:
var users= {
"someone1@example.com": {pass:"one"},
"someone2@example.com": {pass:"two"}
};
//You can store more user related information like firstname, lastname etc. in the above JSON and accessing the data will be very easy for particular user e.g. users["someone1@example.com"].pass
var user=document.getElementById("t1").value;
var pass=document.getElementById("t2").value;
if(users[user] && pass == users[user].pass) {
alert("You are logged in as : " + user);
}