如何使用localStorage / Javascript记住信息(输入值)

时间:2016-03-04 10:12:32

标签: javascript jquery html5 local-storage

我需要一些localstorage / JS的帮助。我想要它,以便当我输入名称/数字(值)并提交时,它必须在我重新访问页面时自动填写这些值(我不想按第一个字母提出建议 - 它必须始终显示默认情况下最后输入的值。

但我绝对不知道该寻找什么。我尝试了很多“本地存储”代码,但似乎没有一个对我有用。

<div id="formular">
  <div id="wrap">
    <div id="formulartekst">
      <form>
        <h2 class="formskrift">Personal Information</h2>
        <input class="oplysninger" type="text" name="inputBox" placeholder="First Name">
        <br>
        <input class="oplysninger" type="text" name="inputBox" placeholder="Second Name">
        <br>
        <input class="oplysninger" type="text" name="inputBox" placeholder="Email or Number">
        <br>
        <textarea class="oplysninger" id="kommentar" name="Text1" cols="30" rows="5" placeholder="Comment"></textarea>
        <input type="submit" value="Order">

1 个答案:

答案 0 :(得分:1)

尝试更新fiddle

function submit()
{
   var text1 = document.getElementById( "text1" ).value;
   var text2 = document.getElementById( "text2" ).value;
   var text3 = document.getElementById( "text3" ).value;
   var text4 = document.getElementById( "kommentar" ).value;
   localStorage.setItem( "text1", text1 );
   localStorage.setItem( "text2", text2 );
   localStorage.setItem( "text3", text3 );
   localStorage.setItem( "text4", text4 );
   console.log( localStorage.getItem( "text1" ));
   return false;
}

window.onload = function()
{
   document.getElementById( "text1" ).value = localStorage.getItem( "text1" );
   document.getElementById( "text2" ).value = localStorage.getItem( "text2" );
   document.getElementById( "text3" ).value = localStorage.getItem( "text3" );
   document.getElementById( "kommentar" ).value= localStorage.getItem( "text4" );   
}
相关问题