localStorage覆盖/和innerHTML失败

时间:2016-12-09 13:47:01

标签: javascript innerhtml

在第14行和第28行,它没有改变标签标签的文本,在recorderForm或loginSetup上,数据被覆盖。
我尝试用.key循环,但它输出错误的存储结果,我不知道我做错了什么。
所以我就把它留在了这里。
我想知道的是为什么标签的innHTML不会改变。(我认为它是由于装载)

window.onload = alert('Window Loaded');

    function recordForm() {
      var userMail = document.getElementById('email').value;
      var userPass = document.getElementById('password').value;
      var confirm = document.getElementById('confirmation').value;
      var text = document.getElemetsByClassName('errorText');

      //Check that the email is not taken and confirm validation
      if ((userPass === confirm)) {
        localStorage.setItem('emailz', userMail.value);
        localStorage.setItem('passwordz', userPass.value);
      } else {
        text.innerHTML = 'Password does not match!'; //line 14
      }
    }

    function loginSetup() {
      var mail = localStorage.getItem('emailz');
      var pass = localStorage.getItem('passwordz');
      var mailInput = document.getElementById('logEmail').value;
      var passInput = document.getElementById('logPassword').value;

      if ((mailInput === mail) && (passInput === pass)) {
        alert(mail);
        alert(pass); 
        } else {
              text.innerHTML = 'Invalid login'; //line 28
        alert('no dice');
        alert(mail);
        alert(pass);
        }
    }

1 个答案:

答案 0 :(得分:1)

您应该尝试存储userMail.value和userPass.value。两者都已经是价值观了:

var userMail = document.getElementById('email').value; //Those are values already
var userPass = document.getElementById('password').value; //Those are values already

if ((userPass === confirm)) {
        localStorage.setItem('emailz', userMail); //Remove the .value
        localStorage.setItem('passwordz', userPass); //Remove the .value
}

另外getEleme n tsByClassName返回一个集合,因此你想要选择它的第一项(可能):

var text = document.getElemetsByClassName('errorText'); //Misstyped and no index.. wont work

var text = document.getElementsByClassName('errorText')[0]; //Should work
var text = document.querySelector('.errorText'); //Would prefer that one

最后在函数loginSetup()中你必须重新定义文本:

var text = document.querySelector('.errorText');