简体   繁体   English

html5 localstorage没有保存输入

[英]html5 localstorage not saving input

I'm trying to safe a value in localstorage to use it later on other pages, but I can't quite get it to work. 我试图在localstorage中保护一个值,以便稍后在其他页面上使用它,但我无法让它工作。

My input field: 我的输入字段:

<input type="text" name="test" value="" id="test" required />
         <span class="highlight"></span>
         <span class="bar"></span>
         <label>enter something</label>

My script: 我的剧本:

<script>

function getStorage(type) {
  var storage = window[type + 'Storage'],
    delta = 0,
    p = document.createElement('p');

  if (!window[type + 'Storage']) return;

  if (storage.getItem('value')) {    
    p.innerHTML = type + 'Storage: ' + storage.getItem('value');
  } else {
    p.innerHTML = type + 'nothing safed';
  }

  document.querySelector('#output').appendChild(p);
}

getStorage('test');


addEvent(document.querySelector('#test'), 'keyup', function () {
  localStorage.setItem('value', this.value);
});


</script>

My output: 我的输出:

    <p id="output"></p>

Note: I'm using h5utils.js ( https://java.net/projects/prototype/sources/svn/content/trunk/demosprojects/AjaxAdmin/web/h5utils.js?rev=11 ) to make it all work, but I can't get output. 注意:我正在使用h5utils.js( https://java.net/projects/prototype/sources/svn/content/trunk/demosprojects/AjaxAdmin/web/h5utils.js?rev=11 )来完成所有工作,但我无法得到输出。 No errors in the console. 控制台中没有错误。

You're calling the getStorage function passing a type of 'test', which then attempts to look up window.testStorage which doesn't exist of course. 你正在调用getStorage函数传递一种'test',然后尝试查找当然不存在的window.testStorage。

Use... 采用...

getStorage('local');

... instead to look up localStorage. ...而不是查找localStorage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM