js中的本地存储将被覆盖

时间:2019-09-11 06:18:35

标签: javascript html json

我正在设置一个登录页面,并希望将所有用户详细信息存储在本地存储中。我的数据被覆盖,无法存储为数组。

我尝试使用stringify推送它,但是它不起作用。

我希望它是用户1,用户2,而不是用户1覆盖用户2

var name1 = new Array();
var pw = new Array();
var x = 0;
// storing input from register-form
function registervalidate() {
  name1[x] = document.getElementById('name1').value;
  pw[x] = document.getElementById('pw').value;

  if (name1[x] == "") {
    alert("Please enter a name");
  } else if (pw[x] == "") {
    alert("Please enter a password");
  } else {

    localStorage.setItem('name1', name1);
    localStorage.setItem('pw', pw);
    alert("Registered Successfully")

  }
  x++;
}


// check if stored data from register-form is equal to entered data in the   login-form
function login() {

  // stored data from the register-form
  var storedName = JSON.parse(localStorage.getItem('name1', JSON.stringify(name1)) || []);
  var storedPw = JSON.parse(localStorage.getItem('pw', JSON.stringify(pw)) || []);

  // entered data from the login-form
  var userName = document.getElementById('userName');
  var userPw = document.getElementById('userPw');

  // check if stored data from register-form is equal to data from login form
  if (userName.value == storedName && userPw.value == storedPw) {
    alert('Successfully Logged in ');
    window.location.assign("Apply Now.html");
  } else {
    alert('Error! Enter correct Username and Password');
  }
}

0 个答案:

没有答案