简体   繁体   English

HTML5本地存储未存储密码框中的值

[英]HTML5 Local Storage not storing value from password box

I have written a HTML file, and it contains two entry boxes, one is called username and the other called password. 我已经写了一个HTML文件,它包含两个输入框,一个叫做用户名,另一个叫做密码。 Something like this: 像这样:

<input class='registerinput' id='registerusername' placeholder='Username'></input>
<input class='registerinput' id='registerpassword' placeholder='Password' type='password'></input>
<input class='registerinput' id='registerpasswordrepeat' placeholder='Repeat Password' type='password'></input>

And I want to store it into a local storage using these codes. 我想使用这些代码将其存储到本地存储中。

if (document.getElementById('registerusername').value != "") {
    localStorage.username=document.getElementById('registerusername').value }
else if (document.getElementById('registerusername').value == "") {
    alert('You must enter a username.') } 
if (document.getElementById('registerpassword').value != "" && document.getElementById('registerpasswordrepeat').value == document.getElementById('registerpassword').value) {
    localStorage.password==document.getElementById('registerpassword'.value) }
else if (document.getElementById('registerpassword').value == "") {
    alert('You must enter a password') }
if (document.getElementById('registerpasswordrepeat').value != document.getElementById('registerpassword').value) {
    alert('The password did not match.') } 

Everything worked except I cannot save the value in password box. 一切正常,除了我无法将值保存在密码框中。 Is it a HTML5 storage limitation for security or a bug in my code? 是出于安全性的HTML5存储限制还是我的代码中的错误? Thanks. 谢谢。

Before answering, please note that there may be browsers in the wild that don't support this, and you can detect this as follows: 在回答之前,请注意,有些浏览器可能不支持此功能,您可以通过以下方式检测到它:

if(window.localStorage) {
    // do your local storage stuff here
} else {
    // do something else instead as localStorage is not available
}

Aside from this information, here's your typo: 除了这些信息,这是您的错字:

localStorage.password==document.getElementById('registerpassword'.value) }

You're missing the parentheses after the closing ' . 结束'后,您缺少括号。

As jmort253 points out it can be wise to check for browser support even though virtually all browsers supports localStorage (but the one that really need to use your app is the one that doesn't...). 正如jmort253指出的那样,即使几乎所有浏览器都支持localStorage ,检查浏览器的支持也是明智的选择(但真正需要使用您的应用程序的浏览器才不是。)。

And also change this line (double = and end-parenthesis in the wrong place): 并更改此行(在错误的位置加double =和右括号):

localStorage.password==document.getElementById('registerpassword'.value)

to: 至:

localStorage.password = document.getElementById('registerpassword').value;

However, the more "correct" syntax for localStorage is to use: 但是, localStorage更“正确”的语法是使用:

localStorage.setItem(key, value);

which you retrieve with: 您使用以下方法检索:

var value = localStorage.getItem(key);

which gives you the value as String or null if it does not exist (the direct property way will as always give undefined if key does not exist). 这将为您提供Stringnull如果不存在,则直接属性方式将始终提供undefined如果key不存在)。

Another tip to make it easier to avoid typos such as this is to centralize your variables, for example: 避免出现此类错误的另一个技巧是集中变量,例如:

var password1 = document.getElementById('registerpassword').value,
    password2 = document.getElementById('registerpasswordrepeat').value;

if (password1.length > 0 && password1 === password2) { ... }

This way you can discover more easily if an error occurs (and as a bonus it's a bit faster as well). 这样,您就可以更轻松地发现是否发生错误(而且,这样做还快一点)。

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

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