本地存储问题

时间:2013-05-31 00:36:08

标签: javascript html5 local-storage

我正在尝试使用此本地存储脚本。

我需要名字,姓氏和州。我可以只保存一个元素吗?我设置了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>

1 个答案:

答案 0 :(得分: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()