【发布时间】:2021-10-01 11:17:58
【问题描述】:
我在将输入文本保存到本地存储并使用 JS 检索它时遇到问题。这是一个调度程序应用程序,我有多个按小时组织的输入框,用户应该能够将他们的计划输入到文本框中并保存,并且应该在页面重新加载时检索它。 这是我的 JS:
// Add input to local storage-------------------------------------------------------//
const button = document.querySelector('.saveBtn');
const hours = ['9', '10' , '11', '12' , '13', '14', '15', '16', '17'];
function savePlan() {
for (let i = 0; i < hours.length; i++) {
// TEST: console.log(hours[i]);
let input11 = document.getElementById('red').value;
console.log(input11);
};
localStorage.setItem('plan', hours[i].value);
// console.log(plan);
};
button.addEventListener('click', savePlan());
// Retrieves plan from local Storage-----------------------------------------------//
function getPlan() {
return localStorage.getItem('plan');
};
getPlan();
这是我的 HTML 的一部分:(在 0900-1700 小时内我有多个这样的盒子)
<tr class="row" id="11">
<th scope="time" id="hour11"class="time">11:00</th>
<td><input type="text" class="textbox" id="h11input"></td>
<td class="btnContainer">
<button class="saveBtn"><i class="fas fa-save"></i></button>
</td>
</tr>
<tr class="row" id="12">
<th scope="time" id="hour12" class="time">12:00</th>
<td><input type="text" class="textbox" id="h12input"></td>
<td class="btnContainer">
<button class="saveBtn"><i class="fas fa-save"></i></button>
</td>
</tr>
任何帮助表示赞赏。谢谢!
【问题讨论】:
-
你必须把东西变成一个字符串才能把它们放到本地存储中。
-
到底是什么问题?您执行
getPlan(),但没有什么可以处理返回值。比您尝试从for循环之外设置“计划”
标签: javascript arrays for-loop foreach local-storage