0

我试图让这个本地存储脚本工作。

我需要名字、姓氏和州。我只能保存一个元素吗?我设置了 3 行,每个元素一个。

另外,我想使用 jQuery append() 方法。到目前为止,我只能得到工作警报。警报(val);但这不是我想要的,我只能让最后一个元素显示......不是名字,姓氏和状态。

这是我的代码:

<div id="message">

       </div>
       </p>
       <p>
    <script type="text/javascript">

      function saveLocal(){
if (window.localStorage) {
    localStorage.setItem("firstname","myfirstname");
    localStorage.setItem("lastname","mylastname");
    localStorage.setItem("lastname","state");
    alert("The data has been saved locally.");
} else {
    alert("Your Browser does not support LocalStorage.");
}
     }

   function readLocal(){
if (window.localStorage) {
    var val = localStorage.getItem("firstname");
    var val = localStorage.getItem("lastname");
    var val = localStorage.getItem("state");
    $("#message").empty().append(getVal());
}else {
    alert("Your Browser does not support LocalStorage.");
}
   }

  </script>
    </head>
  <body>
   <div id="main">
<input type="button" value="Save Values" onclick="saveLocal()"/>
<input type="button" value="Read Values" onclick="getVal()"/>
    </div>
4

1 回答 1

1

您将所有内容存储到一个名为的变量中val,并且您没有列出调用的方法getVal()

var val = localStorage.getItem("firstname");  //<-- val
var val = localStorage.getItem("lastname");   //<-- val
var val = localStorage.getItem("state");      //<-- val
$("#message").empty().append(getVal());       //<-- What is getVal()
于 2013-05-31T00:37:45.923 回答