简体   繁体   English

将用户输入保存在网站上

[英]saving user input on websites

i am making a website for my workplace and i need the content to remain on all the computers that access the site 我正在为我的工作场所创建一个网站,我需要将其内容保留在访问该网站的所有计算机上

i have the code for it and i just need to make it stay on the site 我有它的代码,我只需要使其停留在网站上

<html>
<body>
<input type='text' id='idea' />
<input type='button' value='add to list' id='add' />
<br>
<ul id='list'></ul>

<script>
document.getElementById("add").onclick = function() {
    //First things first, we need our text:
    var text = document.getElementById("idea").value; //.value gets input values

    //Now construct a quick list element
    var li = "<li>" + text + "&nbsp" + "<input type=checkbox>" + "Ready" + "</input>" + "&nbsp" + "<input type=text>" + "</input>" + "</li>";

    //Now use appendChild and add it to the list!
    document.getElementById("list").innerHTML += li;
}
</script>



</body>
</html>

so it would be amazing if someone can fins me an easy and simple solution. 因此,如果有人能给我找到一个简单的解决方案,那将是惊人的。 Thank you 谢谢

Probably the simplest way to do it: You could use local storage to store the data on the user's machine. 可能是最简单的方法:您可以使用本地存储将数据存储在用户的计算机上。 But keep in mind - the data will be deleted as soon as the user clears their local storage. 但请记住-用户清除本地存储后,数据将被删除。

// Get stored text (undefined if nothing stored)
const storedIdea = localStorage.getItem("storedIdea");

// Store text
localStorage.setItem("storedIdea", text);

If you want something more persistent you'll have to use authentication and a backend (you could give Firebase a try!). 如果您想要更持久的功能,则必须使用身份验证和后端(可以尝试Firebase!)。

Window.localStorage is what you can use to locally store data even after the page unloads. 即使页面卸载后,也可以使用Window.localStorage在本地存储数据。 The data will remain, be accessible, and modifiable every time you load and unload the page. 每次加载和卸载页面时,数据将保持,可访问和可修改的。

Create a JavaScript object with the values you want to store. 使用您要存储的值创建一个JavaScript对象。 (Can be an object or a single variable) (可以是一个对象或单个变量)

var userObj = {
        "name": "John",
        "age": "30"
}

Set the object or variable to the localStorage 将对象或变量设置为localStorage

localStorage.setItem("userDetails", userObj);

Use the same userDetails keyword to access the storage values. 使用相同的userDetails关键字访问存储值。

var details = localStorage.getItem("userDetails");

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

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