我试图将输入到表单中的值保存到本地存储 当我检查本地存储时,我正在使用的密钥与其中的[]
一起只有值将阵列的值存储到Chrome本地存储并检索它们
这是否意味着它作为一个数组输入?它只是不填充数组?
我读过,我使用stringify to setItem and parse to getItem
如何从数组中添加值在localStorage的键,然后检索这些?我已经写了产生任何错误的解决方案,但它不进入任何东西,但keyWords []
此外,我不能用我的当前程序检索anyhting但我尝试用
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>
我加了我的html。而关键是被添加到本地存储,它只是没有添加任何值我推动arrat – wuno
好吧跑在我的labtop上,价值被存储在本地存储。我的第一次修正似乎是,变量的错误标签似乎导致了问题。 –
同意迈克尔,我没有注意到。 – Will