英文:
Cookie remembers that a checkbox has been ticked?
问题
我想要创建一个待办事项列表风格的网页,但我不希望用户必须登录才能记住他们已经完成的事项。这个待办事项列表有一个非常特定的用例,我不打算详细介绍。
所以,假设我有一个类似下面的<input type="checkbox">复选框列表:
待办事项:
[✔] 任务1
[ ] 任务2
[ ] 任务3
我已经勾选了任务1,表示它已完成。
是否有一种方法可以设置一个Cookie,以便当用户返回时,任务1仍然被选中,而任务2和任务3仍然未被选中?当任务2和任务3不可避免地被选中或取消选中时,这个Cookie需要进行更新。
英文:
I'm looking to create a to-do list style webpage but I don't want users to have to sign in to remember things they've ticked off. Very specific use case for this checklist which I won't get into.
So, let's say I have a <input type="checkbox"> check list like so:
To do:
[✔] Task 1
[ ] Task 2
[ ] Task 3
I've ticked Task 1 to say that it's complete.
Is there a way I can set a cookie so when the user returns, Task 1 is still checked and Task 2 and 3 are still unchecked? This would need to update when Task 2 and Task 3 are inevitably checked or unchecked.
答案1
得分: 1
是的,我使用HTML和JavaScript成功解决了这个问题:
<!DOCTYPE html>
<html>
<head>
<title>待办事项列表</title>
</head>
<body>
<h1>待办事项:</h1>
<label><input type="checkbox" id="task1"> 任务1</label><br>
<label><input type="checkbox" id="task2"> 任务2</label><br>
<label><input type="checkbox" id="task3"> 任务3</label><br>
<script src="script.js"></script>
</body>
</html>
// 设置cookie的函数
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); // 30天过期
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// 根据名称获取cookie的函数
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// 更新复选框和cookie状态的函数
function updateStatus(taskId) {
var taskElement = document.getElementById(taskId);
var isChecked = taskElement.checked ? "true" : "false";
setCookie(taskId, isChecked);
}
// 从cookie初始化复选框状态的函数
function initializeStatus() {
for (var i = 1; i <= 3; i++) {
var taskId = "task" + i;
var cookieValue = getCookie(taskId);
if (cookieValue === "true") {
document.getElementById(taskId).checked = true;
}
document.getElementById(taskId).addEventListener("change", function() {
updateStatus(this.id);
});
}
}
// 页面加载时调用初始化函数
initializeStatus();
这个代码的作用是设置和获取cookie。当复选框被点击时,它的状态被存储在cookie中。当页面重新加载时,复选框的状态从cookie中读取并相应地恢复。
英文:
Yes, I managed to solve it with HTML & Javascript:
<!DOCTYPE html>
<html>
<head>
<title>Todo List</title>
</head>
<body>
<h1>To do:</h1>
<label><input type="checkbox" id="task1"> Task 1</label><br>
<label><input type="checkbox" id="task2"> Task 2</label><br>
<label><input type="checkbox" id="task3"> Task 3</label><br>
<script src="script.js"></script>
</body>
</html>
// Function to set a cookie
function setCookie(cname, cvalue) {
var d = new Date();
d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000)); // 30 days expiry
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// Function to get a cookie by name
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Function to update checkbox and cookie status
function updateStatus(taskId) {
var taskElement = document.getElementById(taskId);
var isChecked = taskElement.checked ? "true" : "false";
setCookie(taskId, isChecked);
}
// Function to initialize the checkbox statuses from cookies
function initializeStatus() {
for (var i = 1; i <= 3; i++) {
var taskId = "task" + i;
var cookieValue = getCookie(taskId);
if (cookieValue === "true") {
document.getElementById(taskId).checked = true;
}
document.getElementById(taskId).addEventListener("change", function() {
updateStatus(this.id);
});
}
}
// Call the initialize function when the page loads
initializeStatus();
What this does is to set and get cookies. When a checkbox is clicked, its status is stored in a cookie. When the page is reloaded, the checkboxes statuses are read from the cookies and restored accordingly
答案2
得分: 0
如果您只需要在浏览器中使用该值,可能更简单的方法是使用本地存储,而cookie旨在在服务器端也能读取。
设置数值:
localStorage.setItem("checkbox1", value)
读取数值:
const checkbox1Value = localStorage.getItem('checkbox1') ? Boolean(localStorage.getItem('checkbox1')) : false
英文:
If you only need the value in the browser it might be simpler to use localstorage, cookies are intended to be read also on server side.
To set the value:
localStorage.setItem("checkbox1", value)
To read the value:
const checkbox1Value = localStorage.getItem('checkbox1') ? Boolean(localStorage.getItem('checkbox1')) : false
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论