将保存的数据从本地存储显示到html页面

时间:2016-07-31 07:35:18

标签: javascript html

我正在做一个学校项目,我在其中创建网页以允许用户输入然后在另一个页面上显示它。在“创建活动”页面上是一个表单。我已将所有输入保存到本地存储中,但现在我不确定如何检索要在另一个名为“事件描述”的页面上显示的数据。这是我的代码:

function saveToStorage() {
     var nameofevent=document.getElementById("name").value;
     var pList=document.getElementsByName("pos");
     var positions=[];

     for (i=0; i<pList.length; i++){
     positions.push(pList[i].value);
     console.log(pList[i].value);
     }

     //for (i=0; i<positions.length; i++){
     //console.log(positions[i].value);
     //}

     var venue= document.getElementById("venue").value;
     var date=document.getElementById("date").value;
     var starttime=document.getElementById("timeStart").value;
     var endtime=document.getElementById("timeEnd").value;
     var contact=document.getElementById("contact").value;
     var email=document.getElementById("email").value;
     var desc=document.getElementById("desc").value;

     var one={"name":nameofevent,"pos":positions,"venue":venue,"date":date,"timeStart":starttime,"timeEnd":endtime,"contact":contact,"email":email,"desc":desc};
     localStorage["CreateEvent"]=JSON.stringify(one);


     return false;
 }

1 个答案:

答案 0 :(得分:2)

此:

localStorage["CreateEvent"]=JSON.stringify(one);

......应该是:

localStorage.setItem('CreateEvent', JSON.stringify(one));

......反之则是:

var one = JSON.parse(localStorage.getItem('CreateEvent'));