我尝试使用以下代码为记录生成多个密钥(user_0,user_1,user_3 ...)(用户名,密码,电子邮件)我获取警报"数据得救了#34;但没有任何东西保存在本地存储中。有人帮助我在哪里出错了?
$("#submit").click(function(){
if ('localStorage' in window && window['localStorage'] !== null) {
try {
e=$("#email").val();
u=$("#username").val();
p=$("#password").val();
function users(email,username,password){
this.email=email;
this.username=username;
this.password= password;
}
function createNewUser(eml,user,pass) {
var createdUser = new users(e,u,p);
//console.log(createdUser);
if ( localStorage.userCount == undefined ) {
localStorage.setItem('userCount', 0)
}
var userSize = parseInt(localStorage.userCount) + 1;
commitToStorage(userSize,createdUser);
}
function commitToStorage(objectCount,newObject) {
// The unique key of the object:
var item = 'user' + objectCount;
localStorage.setItem('userCount', objectCount);
// Put the object into storage
localStorage.setItem(item, JSON.stringify(users));
}
alert("The data was saved.");
return true;
}catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!');
}
}
} else {
alert('Cannot store user preferences as your browser do not support local storage');
}
});
答案 0 :(得分:1)
您最好的选择是将所有用户对象存储在数组中,如果您愿意,可以使用数组索引为其添加标识符
$("#submit").click(function(){
if ('localStorage' in window && window['localStorage'] !== null) {
var users;
if(!localStorage.users) { users = []; }
else {
users = JSON.parse(localStorage.users);
}
// don't stringify the data yet
var currentUser = {
"userNumber" : users.length,
"email" : $("#email").val(),
"username" : $("#username").val(),
"password" : $("#password").val(),
};
users.push(currentUser);
try {
localStorage.users = JSON.stringify(users);
console.log("The data was saved.");
return true;
}
catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!');
}
}
else {
alert('Cannot store user preferences as your browser do not support local storage');
}
});
答案 1 :(得分:0)
我建议您使用数组结构来存储所有用户数据,而不是创建新密钥:
$("#submit").click(function () {
if ('localStorage' in window && window['localStorage'] !== null) {
try {
//Get stored data
var users = JSON.parse(localStorage.getItem('users'));
if (users === null) {
//If no stored data, create empty array
users = [];
}
//Create new user entry
user = {
email: $("#email").val(),
username: $("#username").val(),
password: $("#password").val(),
};
//Add new entry to stored array
users.push(user);
//Save array
localStorage.setItem("users", JSON.stringify(users));
alert("The data was saved.");
return true;
} catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!');
}
}
} else {
alert('Cannot store user preferences as your browser do not support local storage');
}
});
答案 2 :(得分:0)
问题解决了:D
$("#submit").click(function(){
if ('localStorage' in window && window['localStorage'] !== null) {
try {
e=$("#email").val();
u=$("#username").val();
p=$("#password").val();
createNewUser = function (eml,user,passw) {
var createdUser = new users(eml,user,passw);
if ( localStorage.userCount == undefined ) {
localStorage.setItem('userCount', 0)
}
var userSize = parseInt(localStorage.userCount) + 1;
commitToStorage(userSize,createdUser);
}
function commitToStorage(objectCount,newObject) {
// The unique key of the object:
var item = 'user' + objectCount;
localStorage.setItem('userCount', objectCount);
// Put the object into storage
localStorage.setItem(item, JSON.stringify(newObject));
}
function users(email,username,password){
this.email=email;
this.username=username;
this.password= password;
}
createNewUser(e,u,p);
alert("The data was saved.");
return true;
}catch (e) {
if (e == QUOTA_EXCEEDED_ERR) {
alert('Quota exceeded!');
}
}
} else {
alert('Cannot store user preferences as your browser do not support local storage');
}
});