【问题标题】:Localstorage in webbrowser浏览器中的本地存储
【发布时间】:2016-09-16 21:08:52
【问题描述】:

我有一个输入类型的文本框值将在按钮提交时存储在本地存储中

<html>
<body action="Servlet.do" >
<input type="text" name="a"/>
<button type="submit"></button>
</body>
</html>

提交按钮后如何存储在本地存储中?

【问题讨论】:

  • 您单击按钮并提交表单。您需要添加一个事件侦听器click,然后使用 localstorage 执行您的编写逻辑。

标签: javascript jquery html local-storage


【解决方案1】:

提交按钮的onclick功能请执行以下代码以将文本框值存储在本地存储中,键为“a”。

localStorage.setItem("a", document.getElementsByName('a').value);

请查看下面的 sn-p 以获得更多理解。

HTML

<html>
  <body action="Servlet.do" >
    <input type="text" name="a"/>
    <button type="submit" onclick="storeVal();">Submit</button>
  </body>
</html>

Javascript

function storeVal(){
  localStorage.setItem("a", document.getElementsByName('a').value);
}

【讨论】:

    【解决方案2】:

    您可以从此列表http://caniuse.com/#search=localstorage 中检查目标浏览器是否支持本地存储。然后您可以使用 localStorage.setItem 设置一个值并使用 localStorage.getItem 来检索它。

    【讨论】:

      【解决方案3】:

      你也可以这样做:

      本地存储用作键/值,我使用 [inputValue] 作为键

      jQuery

      localStorage.inputvalue = $('input').val()
      

      要从 LocalSorage 获取值,您可以这样做:

      $('input').val(localStorage.inputvalue)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-31
        • 2021-12-16
        • 2014-02-05
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多