【发布时间】:2021-02-11 21:12:12
【问题描述】:
const firstName = document.getElementById("firstName");
const lastnName = document.getElementById("lastName");
const email = document.getElementById("newEmail");
const password = document.getElementById("newPassword");
const btnSignup = document.getElementById("btn-signup");
btnSignup.onclick = function () { // when mouse click "signup" button
const first_name = firstName.value; // getting the value of firstName and so on..
const last_name = lastName.value;
const e_mail = newEmail.value;
const pass_word = newPassword.value;
if (first_name && last_name && e_mail && pass_word) {
//set user input into JSON
let user_data = {
firstName: first_name,
lastName: last_name,
email: e_mail,
password: pass_word
}
//get to localstorage if there is existing user ||or make empty array[]
let clientsArr = JSON.parse(localStorage.getItem('users')) || [];
//ipush ang new user sa array
clientsArr.push(user_data);
//save to localstorage
localStorage.setItem('users', JSON.stringify(clientsArr));
}
// location.reload();
else if (user_data === localStorage.getItem('users')) { // THIS IS THE CODE THAT I AM TRYING
alert("User already existed");
}
};
我正在尝试在屏幕上显示错误消息或在浏览器上显示警告,此代码确实有效,问题是如何识别本地存储中是否存在现有用户。我想说的是,如果内存中有现有用户,您的“登录”将不接受。谢谢!我对要运行的代码添加了注释。
【问题讨论】:
-
试试这样的
if (localStorage.getItem("user") === null) { // your code goes here }
标签: javascript html arrays dom local-storage