- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 localStorage 存储用户输入值,然后在倒计时器中使用该值。
这是用户输入数据的 HTML:
<form action='/' method= 'GET' id='settings'>
<label> Work Time </label>
<input class='settingInput' type='number' id='workInput'>
<label id='short-break'> Short Break </label>
<input class='settingInput' id='shortBreak'>
<label id='long-break'> Long Break </label>
<input class='settingInput' id='longBreak'>
<button id = 'set-values'>Submit</button>
</form>
这是用于存储和检索数据的 JavaScript:
var workInputValue = document.getElementById('workInput').value;
var workTimeSerialized = JSON.stringify(document.getElementById('workInput').value);
var workTimeFinal = JSON.parse(localStorage.getItem('workTimeKey'));
submitButton.addEventListener('click', (e) => {
localStorage.setItem('workTimeKey', workTimeSerialized);
console.log('submit pressed');
e.preventDefault();
})
这是整个项目的代码笔:https://codepen.io/Games247/pen/XWJqebG
这是我第一次使用 setItem 和 getItem,所以我可能会忽略一些明显的东西。
目前看起来一对括号存储在 localStorage 中,workTimeKey 应该在该位置。
最佳答案
您在 codepen 上的链接代码有问题,事实上,此处发布的代码纠正了所述问题。
var workTimeSerialized = JSON.stringify(document.getElementById('workInput'));
上面是您的代码笔,问题是您正在尝试将 HTML 元素序列化为 JSON,而不是它的值,因此您会在 session 存储中看到“{}”。
您需要确保它是输入元素的值,而不是您序列化的元素本身。就像我提到的,您在此处发布的代码解决了该问题;
var workTimeSerialized = JSON.stringify(document.getElementById('workInput').value);
注意:每当您在 session 存储中看到“[]”或“{}”而不是您想要的值时,您要么直接传递一个对象,要么传递一个元素。
编辑:“你很可能两者都不是”
您的输入值应该在提交点击处理程序中读取,否则,您将在提交之前而不是之后获得输入的值
所以你的代码:
var workInputValue = document.getElementById('workInput').value;
var workTimeSerialized = JSON.stringify(document.getElementById('workInput').value);
var workTimeFinal = JSON.parse(localStorage.getItem('workTimeKey'));
submitButton.addEventListener('click', (e) => {
localStorage.setItem('workTimeKey', workTimeSerialized);
console.log('submit pressed');
e.preventDefault();
})
变成:
submitButton.addEventListener('click', (e) => {
var workInputValue = document.getElementById('workInput').value;
var workTimeSerialized = JSON.stringify(document.getElementById('workInput').value);
var workTimeFinal = JSON.parse(localStorage.getItem('workTimeKey'));
localStorage.setItem('workTimeKey', workTimeSerialized);
console.log('submit pressed');
e.preventDefault();
})
关于javascript - 为什么我的 localStorage.setItem() 和 localStorage.getItem() 没有存储值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851688/
我有一个适用于 Mac 和 Linux 用户但不适用于 Windows 用户的 Chrome 扩展程序。当我进入控制台并在 localStorage 中设置一个项目时,它工作正常,但是当用户尝试使用扩
当我尝试将项目设置到 tableView 中时遇到问题,有关我使用 SceneBuilder 的信息。 Main.java: public class Main extends Application
我使用下面的代码在 webstorage 对象中存储一些内容,但出现此错误: Error:-- Object doesn't support property or method 'setItem'
我的 UINavigationController 工具栏是空白的,上面没有显示任何项目。这是我的代码 self.navigationController.toolbarHidden = NO; UI
创建 JavaFX GUI 应用程序,但无法让 ListView 在我的 Controller 类上工作。这是代码: MP3.Java package mp3player; import javafx
我的团队正在进行的一个小型学校项目遇到了问题。 每当我将输入提交到本地存储时,它都会覆盖以前的本地存储项目。这个想法是稍后通过使用复选框等“合并”成员输入与任务输入。 我希望能够从本地存储中列出大量名
我有一个实现可初始化的 Controller 类,它看起来像这样: public class FileSharingController implements Initializable { priv
我是 Qt 的新手,真的需要帮助解决这个问题。我目前正在学校开展一个实践培训项目。当我使用 ui->tableWidget->setItem(.....) 但没有显示时,我被卡住了。虽然新行添加了空白
我即将在我的一个 Angular 项目中使用 Storage.setItem(),但想检查当两个单独的 Web 应用程序尝试使用相同的 key 设置存储时浏览器如何处理该场景。 之前的值是否会被覆盖?
当使用 IE11 并使用 javascript 的 localStorage 功能时,由于浏览器内的安全设置,浏览器控制台会抛出“拒绝访问”消息来阻止 javascript 的执行。 有没有办法通过
我的 ListView 适配器中有这个, public void setListItems(List data) { Log.d(tag, this.data.size() + "," + d
所以,这是我的代码。 def classMaker(csv): csv = csv.split("/n") firstLine = csv[0] csv = csv[1:]
我正在尝试使用 HTML5 本地存储做一个 super 简单的 setItem 和 getItem。它似乎只是行不通。这有效: $(document).ready(function () { lo
我正在通过 WPF 应用程序创建一个 CRUD 应用程序来学习基础知识。但是,当我调用 SetItem() 来更改 ObservableCollection 中的元素时,我遇到了“由于其保护级别而无法
这个问题在这里已经有了答案: localStorage - use getItem/setItem functions or access object directly? (5 个答案) 关闭 7
每次单击箭头时,React 状态都会保存在本地ٍ 存储中 但是当你刷新页面时,值会减少 1,因为最后的更改没有保存到本地存储 如果修改多个项目,问题将在最后按下的项目The Code is here
我正在尝试将数组存储在客户端浏览器的 localStorage 中,以便在页面加载后轻松访问内容,数组是从数据库中提取的。 我的问题是,我只能在页面重新加载并反射(reflect)在客户端后才能使用本
下面是我的函数。我可以通过 res 在 console.log 中看到一个对象: login2(){ console.log(this.user_form_value); consol
好吧,假设我在 HTML 页面中输入这两个值:20、30。 我有这两个变量,我从 HTML 页面获取值,并且工作正常,因此 BrugerFugt=20 和 BrugerTemp=30: Bru
我正在尝试创建一个使用 localStorage 的图书跟踪器应用程序。但是,当我设置一个项目时 - 发生的情况是没有给出错误消息,但它不会记录到 localStorage。这是我的代码:
我是一名优秀的程序员,十分优秀!