将数组的值存储到Chrome本地存储&取回它们
Storing Values of An Array to Chrome Local Storage & Retrieving Them
我正在尝试将输入表单的值保存到本地存储中,当我检查本地存储器时,我使用的密钥和[]
一起在其中,作为其唯一值
这是否意味着它以数组的形式进入?它只是不填充数组?
正如我所读到的,我正在使用stringify to setItem and parse to getItem
如何将数组中的值添加到本地存储中的键中,然后检索它们?我写了一个解决方案,它不会产生错误,但除了keyWords []
之外,它什么都不输入此外,我无法用我当前的程序检索任何东西,但我尝试使用
loadKeyWords();
这是我的代码,
localArray = [];
localStorage.setItem('keyWords', JSON.stringify(localArray));
function setArrayInLocalStorage(keyWords, localArray) {
localStorage.setItem(key, JSON.stringify(localArray));
}
function getArrayInLocalStorage(keyWords) {
return JSON.parse(localStorage.getItem(keyWords));
}
function loadKeyWords() {
$('#keyWords').html('');
localArray = getArrayInLocalStorage('keyWords');
//for all the items in the array...
for(var i = 0; i < localArray.length; i++) {
//add them to the UL
$('#keyWords').append('<li><input id="check" name="check" type="checkbox">'+localArray[i]+'</li>');
}
}
$('#add').click( function() {
var Description = $('#description').val();
if($("#description").val() === '') {
$('#alert').html("<strong>Warning!</strong> Enter some words you hate!");
$('#alert').fadeIn().delay(1000).fadeOut();
return false;
}
$('#keyWords').prepend("<li><input id='check' name='check' type='checkbox'/>" + Description + "</li>");
$('#form')[0].reset();
localArray.push(Description);
setArrayInLocalStorage('keyWords', localArray);
loadKeyWords();
return false;
});
$('#clear').click( function() {
window.localStorage.clear();
location.reload();
return false;
});
loadKeyWords();
这是我的HTML
<!doctype html>
<html>
<head>
<title>Wuno Zensorship</title>
<script src="jquery-1.11.3.min.js"></script>
<script src="popup.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<img src="icon48.png">
<section>
<form id="form" action="#" method="POST">
<input id="description" name="description" type="text" />
<input id="add" type="submit" value="Add" />
<button id="clear">Clear All</button>
</form>
<div id="alert"></div>
<ul id="keyWords"></ul>
</body>
</html>
几件事:
对于您的代码:
function setArrayInLocalStorage(keyWords, localArray) {
localStorage.setItem(key, JSON.stringify(localArray));
}
应该是:
function setArrayInLocalStorage(keyWords, localArray) {
localStorage.setItem(keyWords, JSON.stringify(localArray));
}
键未定义。
我也不确定你的html是什么样子的,因为它没有给我们。你确定你是在为Description传递一个值,并且在添加函数时实际调用了这个函数吗?我会在元素add上的点击事件上放一个控制台日志,以确保函数被调用,我还会控制台日志变量Description的值。
也有可能,任何id为"add"的元素都是一个提交按钮,这意味着它将触发刷新事件,因此您必须使用$('#add').click( function(e) {
e.preventDefault();
这是我测试的html,这些项目确实出现在localStorage中。检查您的开发人员工具和资源,以确保它留在本地存储:
<div id="keywords">
<form id="form">
<input id="description" type="text" name="firstname">
<button id="add">Submit</button>
</form>
</div>
localArray = [];
// 1. For consistency sake, you should call setArrayInLocalStorage to store the array
// 2. Just so that you know, everytime your app starts, you store an empty array into it
localStorage.setItem('keyWords', JSON.stringify(localArray));
function setArrayInLocalStorage(keyWords, localArray) {
localStorage.setItem(key, JSON.stringify(localArray));
}
function getArrayInLocalStorage(keyWords) {
return JSON.parse(localStorage.getItem(keyWords));
}
function loadKeyWords() {
// 3. I suggest you create local variable and store $('#keyWords') in it and use that variable in this function, more efficient that way
$('#keyWords').html('');
localArray = getArrayInLocalStorage('keyWords');
//for all the items in the array...
for (var i = 0; i < localArray.length; i++) {
//add them to the UL
$('#keyWords').append('<li><input id="check" name="check" type="checkbox">' + localArray[i] + '</li>');
}
}
$('#add').click(function() {
// 4. Try to stick with convent and define variable using camel case
var Description = $('#description').val();
// 5. You already have the string/empty string in Description, you can simply use it here to compare with ''
if ($("#description").val() === '') {
$('#alert').html("<strong>Warning!</strong> Enter some words you hate!");
$('#alert').fadeIn().delay(1000).fadeOut();
return false;
}
// 6. Not sure why you take the time to build the list in html, yet to rebuild it in the function call below
$('#keyWords').prepend("<li><input id='check' name='check' type='checkbox'/>" + Description + "</li>");
// 7. Because we don't have your html code, not sure what this does and why you'll need it
$('#form')[0].reset();
localArray.push(Description);
setArrayInLocalStorage('keyWords', localArray);
loadKeyWords();
return false;
});
$('#clear').click(function() {
// 8. You might want to consider removing the keyWords instead of clearing your local storage here
window.localStorage.clear();
location.reload();
return false;
});
loadKeyWords();
除了我的八条评论之外,我不明白为什么你应该将关键字存储在本地存储中并检索。如果你喜欢,请添加你的html代码,我们可以提供进一步的帮助。
相关文章:
- jquery点击函数select&取消选择
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- Angular js-返回一个包含类似
- 一点javascript元编程&可链接的设置器
- 将作用域存储在JSON中
- 使用javascript存储变量的最安全方式
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 如何有效地将游戏数据存储在URL查询字符串中
- Ember.js-接口状态应该存储在哪里
- 通过JSON&比较时间
- Jquery模板,如果xx&&如果yy
- 如何存储&读取输入字段中的任意url
- @RequestParam不接受'&'在从请求发送的字符串中,存储&并截断其余部分
- 将数据存储在模型&集合呼叫&取来
- 如何存储鼠标&键盘事件和值
- 如何存储拖动&将项目放入cookie,然后在另一个页面中检索
- Mongodb&Espress:将对象存储在模型中
- 将数组的值存储到Chrome本地存储&取回它们
- 存储/导入变量&函数处理(JS)