我正在尝试使用localStorage创建一个If / Else语句但遇到问题。 我似乎无法将If语句链接到我的localStorage用户并始终获得Else语句。 如何迭代localStorage键并获取用户的值?
如果我能以某种方式定位ID 1,ID 2,ID 3等,那对我来说是有意义的但是如何?
IF / ELSE代码:
User.$button_login.addEventListener("click", function(entry) {
for (var i = 0, len = localStorage.length; i < len; ++i) {
console.log(localStorage.getItem(localStorage.key(i)));
}
if (document.getElementById("firstName").value == User.$form.user_name.value) {
if (document.getElementById("passWord").value == User.$form.password.value) {
alert("You have logged in");
}
} else {
document.getElementById("negative").innerHTML = "Username or Password does not match"
}
}, true);
},
用户代码:
var User = {
index: window.localStorage.getItem("User:index"),
$form: document.getElementById("userReg"),
$button_register: document.getElementById("registerUser"),
$button_login: document.getElementById("logIN"),
init: function() {
// initialize storage index
if (!User.index) {
window.localStorage.setItem("User:index", User.index = 1);
}
User.$form.addEventListener("submit", function(event) {
var entry = {
id: parseInt(this.id_entry.value),
user_name: this.user_name.value,
password: this.password.value
};
if (entry.id == 0) {
User.storeAdd(entry);
}
}, true);
storeAdd: function(entry) {
entry.id = User.index;
window.localStorage.setItem("User:index", ++User.index);
window.localStorage.setItem("User:" + entry.id, JSON.stringify(entry));
},
};