【发布时间】:2018-04-14 06:53:25
【问题描述】:
我正在尝试显示以前搜索过的位置列表。我在整个网站上使用会话存储,以及专门用于搜索历史的本地存储。
目前
if (typeof(Storage) !== "undefined") {
//saves name in local storage (for histoy list)
localStorage.setItem("name", currentWeather.name);
//saves name in session storage - could be other things
sessionStorage.setItem("name", currentWeather.name);
// Retrieve session storage name to display back current location on search page
document.getElementById("name").innerHTML = sessionStorage.getItem("name");
// History
var previous = {
location : $("#name").val(),
}
var record = JSON.stringify(previous.location);
localStorage.setItem(previous.location, record);
for (var i = 0; i < localStorage.length; i++){
$("#record").append(localStorage.getItem(localStorage.key(i)));
}
html:
<p>Location set to: <span id="name"></span></p>
<p>Record:</p>
<a id="record"></a>
所以在尝试放入列表之前,我只想让位置简单地输出。目前它是这样出来的:
""曼彻斯特""爱丁堡
在 chrome 应用程序选项卡上,会话存储显示名称键,并且最新名称出现在那里。它还显示一个值为"" 的空白键,我不明白为什么或如何解决这个问题。
【问题讨论】:
-
因为
$("#name").val()。这是给你null或空字符串,你在这里使用它,即localStorage.setItem(previous.location, record);来设置密钥
标签: javascript jquery local-storage