简体   繁体   English

如何通过注册将传入的详细信息推送到 javascript 中的本地存储

[英]how to push the incoming details through registration for login to the localstorage in javascript

the problem to push data into localstorage from the array with objects, the details getted through textboxes by registeration and login将数据从带有对象的数组推送到本地存储的问题,通过注册和登录通过文本框获取的详细信息

how json used for this problem in javascript json 如何在 javascript 中用于此问题

<body  background="img.jpeg">
    <div><dr><a href="logon.html">LOGIN</a> <a class="a1" href="register.html">REGISTER</a></dr> </div>
    <div class="div1">
    <form id="log">
        <h1>LOGIN</h1>
        <label><b>USERNAME:</b></label><input type="text" name="Username" id="usrnme"><br><br>
        <label><b>PASSWORD:</b></label><input type="text" name="Password" id="pwd"><br><br>
        <label><b>EMAIL-ID:</b></label><input type="text" name="Email" id="mail"><br><br>
        <input type="button" value="Register" name="Register" onclick="register()">
    </form>
</div>

the result to store the data to the local storage将数据存储到本地存储的结果

Basically you can store data with two ways.基本上你可以用两种方式存储数据。

You can use the setItem function您可以使用setItem function

localStorage.setItem(myKey, myValue);

or you can directly set those values via或者您可以通过直接设置这些值

localStorage.myKey = myValue;

If you want to receive values from the localstorage you can use get instead of set.如果要从本地存储接收值,可以使用get而不是 set。

Please let me know if that already helps you.请让我知道这是否对您有所帮助。

You can use, FormData,您可以使用,FormData,

<script>
let frm = document.querySelector("#log");

let data = new FormData(frm);

for (var [key, value] of data.entries()) { 
  localStorage.setItem(key, value);
}

// Finally you can get values from localStorage

localStorage.getItem("key");

</script>

Before run this code make sure form values are not empty.在运行此代码之前,请确保表单值不为空。

You need to create a JS file or <script> tag that can extract the values from the input field and use browser localstorage API to set the values.您需要创建一个可以从输入字段中提取值的 JS 文件或<script>标签,并使用浏览器localstorage API 来设置这些值。

Seeing that your code is still limited to just HTML and I do not know your level of knowledge in HTML and JS, I will add a code snippet to help you along the way:看到您的代码仍然仅限于 HTML 并且我不知道您在 HTML 和 JS 方面的知识水平,我将添加一个代码片段来帮助您:

...html stuff
<script>
  // JS code
</script>

Resources:资源:

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

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