如何使用HTML中的本地存储在Array中存储数据
How to store data in Array using Local storage in HTML?
<!DOCTYPE html>
<html>
<body>
<input type="text" id="textstorage">
<button name="testbutton" onclick="myFunction()">send</button>
<div id="result"></div>
<script>
function myFunction(){
alert("dasd")
// Check browser support
var inp = document.getElementById("textstorage");
if (typeof(Storage) != "undefined") {
// Store
localStorage.setItem("result", inp.value);
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("result");
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
}
}
document.getElementById("result").innerHTML = localStorage.getItem("result");
</script>
</body>
</html>
如何使用数组在网页中存储多个值。请帮帮我。
注意:如果我输入一个值,它将替换以前的值。我需要像数组一样存储所有值。
LocalStorage不支持数组,只支持字符串值。它存储key:value
对。使用JSON.stringify()
和JSON.parse()
将数组间接存储在LocalStorage
对象中。
例如:
var arr = new Array();
arr[0] = "One";
arr[1] = "Two";
arr[2] = "Three";
localStorage["arr"] = JSON.stringify(arr);
var arrStored = JSON.parse(localStorage["arr"]);
相关文章:
- 如何为json对象中的段发送array[]
- 将作用域存储在JSON中
- 使用javascript存储变量的最安全方式
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 如何有效地将游戏数据存储在URL查询字符串中
- Ember.js-接口状态应该存储在哪里
- Array.length似乎不起作用;console.log则显示其他情况
- 如何迭代Array.prototype函数
- 如何解雇“;铁局部存储负载”;事件
- 本地存储中的字符串到字节数组转换
- 从jQuery调用存储在Variable中的函数
- 推荐在JavaScript中执行存储为字符串的函数,而不是使用eval
- Javascript 新的 Array 和 join() 方法
- 通过另一个php应用程序将我的项目推送到Github存储库中
- 2D Array或Map存储工作日和天内槽的值
- 从javascript对象中获取值并将其存储到Array中
- 将XML存储在Array中
- 在Array中存储多个抓取数据
- 如何使用HTML中的本地存储在Array中存储数据