如何使用JavaScript在本地存储用户输入

时间:2018-12-09 12:04:13

标签: javascript html css cross-platform

我正在创建一个跨平台的应用程序,我希望将用户 bio 存储在本地,并在刷新和关闭/重新打开后保留在该位置谢谢

var newBio = document.getElementById("bio").value;
var storeBio = localStorage.setItem(newBio);
document.getElementById("bio").innerHTML = localStorage.getItem(storeBio);
<input id="bio" type="text" name="search" placeholder="Bio" onmouseout="showBotNav(); saveBio()"><br>

1 个答案:

答案 0 :(得分:0)

如果您不知道如何写东西,请查看MDN文档中的examle

.getItem需要一个名称作为参数。 .setItem需要两个(名称和值),但是您只提供了一个参数storeBio,这是不正确的(也是因为函数localStorage.setItem 不能是名字)。

在您的html中,您声明了saveBio函数。为什么不向我们显示保存和获取功能呢?

function saveBio() {
  var newBio = document.getElementById("bio").value;
  localStorage.setItem('bio', newBio);
}

function getBio() {
  // why you were using (correct way) ".value" when setting bio
  // but here you are using .innerHTML ?
  document.getElementById("bio") //.innerHTML =      
    .value = localStorage.getItem('bio')

  // by the way innerHTML is only present on elements with closing tag. Input element is not one of 
}

// here you calling function when page is loaded
getBio()
<input id="bio" type="text" name="search" placeholder="Bio" onmouseout="showBotNav(); saveBio()"><br>

发布的问题表明您没有包括enough effort to solve问题。我们总是很高兴在SO上为新的编码人员提供帮助和支持,但是您需要首先自助。