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
<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>
我在这里尝试做的是密码验证,看看pass1 = pass2,如果它们相等,则进入登录页面并将信息存储到本地存储中,但如果没有,则清除该人的密码字段重新输入密码。
信息不会保存到本地存储中。单击“提交”按钮后,页面将刷新,并且信息在URL中可行但不在本地存储中。我试图坚持HTML和Javascript。
答案 0 :(得分:1)
您缺少执行此操作所需的ID标记:
<input type="password" name="pass1" id="pass1" placeholder="Password" required>
<input type="password" name="pass2" id="pass2" placeholder="Re-type Password" required>
这是因为您正在使用:
document.getElementById("pass1").value = "";
document.getElementById("pass2").value = "";
没有ID,没有任何东西可以抓住该功能。
答案 1 :(得分:0)
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));