简体   繁体   English

信息未保存到本地存储

[英]Information not saving into Local Storage

Java Script Java脚本

function getid() {
    'use strict';
    var pass1, pass2;
    pass1 = document.getElementById("pass1").value;
    pass2 = document.getElementById("pass2").value;
    if (pass1 !== pass2) {
        window.alert("Passwords Do not match");
        document.getElementById("pass1").value = "";
        document.getElementById("pass2").value = "";

    } else {
        logininfo[index] = {
            fname: document.getElementsByName("firstName").value,
            lname: document.getElementsByName("lastNane").value,
            student: document.getElementsByName("studentn").value,
            password: document.getElementsByName("password2").value
        };
        index++;
        localStorage.setItem("login", JSON.stringify(logininfo));
        window.location = "login.html";
    }
}

HTML HTML

<form>
  <div class="login-card">
    <img src="rsz_logo.gif" alt="North Park Logo" >

    <h1>Register</h1><br>
    <input type="text" name="firstName" placeholder="First Name" required>
    <input type="text" name="lastName" placeholder="Last Name" required>
    <input type="text" name="studentn" placeholder="Student Number" required>
    <input type="password" name="pass1" placeholder="Password" required>
    <input type="password" name="pass2" placeholder="Re-type Password" required>

    <input type="submit" name="login" class="login login-submit" value="Register" onclick="getid()">

    <div class="login-help">
      <a href="login.html">Back to Login</a>
    </div>
  </div>
</form>

What I'm trying to do here is password validation to see if pass1 = pass2, if they are equal then proceed to the login page and store the information into the local storage but if not then clear the password fields for the person to retype the passwords. 我要在此处执行的操作是密码验证,以查看pass1 = pass2是否相等,然后进入登录页面并将信息存储到本地存储中,但如果不相同,则清除此人的密码字段以重新输入密码密码。

The information doesn't save into the local storage. 信息不会保存到本地存储中。 After I click the submit button the page refreshes and the information is viable in the url but not in the local storage. 单击提交按钮后,页面将刷新,并且该信息在URL中可用,但在本地存储中不可用。 I am trying to stick to HTML and Javascript. 我试图坚持HTML和Javascript。

You are missing the ID tag needed to do this: 您缺少执行此操作所需的ID标签:

<input type="password" name="pass1" id="pass1" placeholder="Password" required>
<input type="password" name="pass2" id="pass2" placeholder="Re-type Password" required>

This is because you are using: 这是因为您正在使用:

document.getElementById("pass1").value = "";
document.getElementById("pass2").value = "";

With no ID, there's nothing for that function to grab from. 没有ID,该功能将无法获取。

logininfo = {
    fname: document.getElementsByName("firstName").value,
    lname: document.getElementsByName("lastNane").value,
    student: document.getElementsByName("studentn").value,
    password: document.getElementsByName("password2").value
};
localStorage.setItem("login", JSON.stringify(logininfo));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM