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

How do I use localStorage/Javascript to remember information (input values)

本文关键字:信息 输入 localStorage 何使用 Javascript      更新时间:2023-09-26

我需要一些关于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">

试试这个更新的小提琴

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" );   
}