输入未存储在本地存储中

时间:2017-12-02 13:47:04

标签: javascript dom input local-storage

我有问题将文本框中的输入存储到本地存储中,我得到的是“未定义”。我似乎已经按照本地存储的所有教程,但它仍然没有工作。我有一个index.html,app.js和name.js. name.js中的函数应该将输入存储在app.js中发生的点击事件中。我哪里错了?

的index.html:

<form id="nameForm">
  <label>Ditt namn (Max 10 tecken):
    <input type="text" id="name" maxlength="10" />
  </label>
  <input type="button" id="submit" value="Choose" />
</form>

name.js :(并表示未定义localStorage)

function name () {
  let nickname = document.querySelector('#name')
  localStorage.Nickname = nickname.value
  console.log(nickname)
}

module.exports = name

app.js:

const timer = require('./timer.js')
const Quiz = require('./Quiz.js')
const name = require('./name.js')

let start = document.querySelector('#submit')
function quizAndTimer () {
  Quiz()
  timer()
  name()
}
start.onclick = quizAndTimer

2 个答案:

答案 0 :(得分:0)

尝试

localStorage.setItem('Nickname', nickname.value); // this stores 

localStorage.getItem('Nickname') // this retrieves

答案 1 :(得分:0)

而不是:

localStorage.Nickname = nickname.value;

使用:

localStorage.setItem('NickName', nickname.value); //this sets your key/value pair in localStorage.
  

P.S。:localStorage仅保存键/值对格式(键和值)   总是采用字符串格式。

相关问题