gpt4 book ai didi

javascript - 一个简单的todoApp复选框问题

转载 作者:行者123 更新时间:2023-11-28 14:31:21 24 4
gpt4 key购买 nike

我有一个关于 DOM 操作的简单 todoApp。

const classNames = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}

const list = document.getElementById('todo-list')
const itemCountSpan = document.getElementById('item-count')
const uncheckedCountSpan = document.getElementById('unchecked-count')
let count = 0;
let checkedCount = 0;
let todo;
function newTodo() {
todo = prompt("Please enter to do task","enter here")
count++;
const markup = `<li><input type="checkbox" id=${todo} name="feature"
value=${todo} data-ischecked = "false" />
<label for=${todo}>${todo}</label></li>`
list.insertAdjacentHTML("beforeend",markup)
itemCountSpan.innerHTML = count;
document.getElementById(todo).addEventListener("click",check);
}

function check(){
let article = document.getElementById(todo);
if(article.dataset.ischecked == "false"){
article.dataset.ischecked = "true";
checkedCount++;
uncheckedCountSpan.innerHTML = checkedCount
}
else{
article.dataset.ischecked = "false";
checkedCount--;
uncheckedCountSpan.innerHTML = checkedCount
}
}
<!DOCTYPE html>
<html>
<head>
<title>TODO App</title>
<link rel="stylesheet" type="text/css" href="./styles.css" />
</head>
<body>
<div class="container center">
<h1 class="center title">My TODO App</h1>
<div class="flow-right controls">
<span>Item count: <span id="item-count">0</span></span>
<span>Unchecked count: <span id="unchecked-count">0</span></span>
</div>
<button class="button center" onClick="newTodo()">New TODO</button>
<ul id="todo-list" class="todo-list"></ul>
</div>
<script src="./script.js"></script>
</body>
</html>

但是,检查的功能并不是我所期望的。当我单击复选框时,总值应相应增加/减少,但数字在 0 和 1 之间切换。

有什么方法可以解决这个问题吗?谢谢

最佳答案

为什么你的不起作用

您将最近添加的项目的 ID 存储为名为 todo 的变量。这意味着在您的 check() 函数中,该行...

let article = document.getElementById(todo); 

...将始终引用最后添加的项目。它只是在最后一项上来回切换 data-ischecked

<小时/>

如何修复

您希望点击事件引用正在点击的复选框。。我们可以通过向 check() 函数添加一个名为 event 的参数来实现这一点。这样,event.target 将允许我们引用被单击的元素。

function check(event) {
let article = event.target; //This is the clicked checkbox!
}

(鉴于此,todo 变量不再需要是全局变量。)

<小时/>

另一重要说明

我强烈建议不要使用 todo 作为元素 ID,因为您的 ID 中不能有空格。任何时候输入带有空格的待办事项时,您当前的代码都会抛出错误。

鉴于您不允许删除项目,您可以将其设置为 "todo-"+ count 之类的内容,以便您的 ID 为 todo-0todo-1todo-2

我也在下面的代码中实现了这一点,允许您在待办事项中添加空格。

const classNames = {
TODO_ITEM: 'todo-container',
TODO_CHECKBOX: 'todo-checkbox',
TODO_TEXT: 'todo-text',
TODO_DELETE: 'todo-delete',
}

const list = document.getElementById('todo-list')
const itemCountSpan = document.getElementById('item-count')
const uncheckedCountSpan = document.getElementById('unchecked-count')
let count = 0;
let checkedCount = 0;

function newTodo() {
var todo = prompt("Please enter to do task", "enter here");
var newId = "todo" + count;
count++;
const markup = `<li><input type="checkbox" id=${newId} name="feature"
value=${todo} data-ischecked = "false" />
<label for=${todo}>${todo}</label></li>`
list.insertAdjacentHTML("beforeend", markup)
itemCountSpan.innerHTML = count;
document.getElementById(newId).addEventListener("click", check);
}

function check(event) {
let article = event.target;
if (article.dataset.ischecked == "false") {
article.dataset.ischecked = "true";
checkedCount++;
uncheckedCountSpan.innerHTML = checkedCount
} else {
article.dataset.ischecked = "false";
checkedCount--;
uncheckedCountSpan.innerHTML = checkedCount
}
}
<!DOCTYPE html>
<html>

<head>
<title>TODO App</title>
<link rel="stylesheet" type="text/css" href="./styles.css" />
</head>

<body>
<div class="container center">
<h1 class="center title">My TODO App</h1>
<div class="flow-right controls">
<span>Item count: <span id="item-count">0</span></span>
<span>Unchecked count: <span id="unchecked-count">0</span></span>
</div>
<button class="button center" onClick="newTodo()">New TODO</button>
<ul id="todo-list" class="todo-list"></ul>
</div>
<script src="./script.js"></script>
</body>

</html>

<小时/>

关于javascript - 一个简单的todoApp复选框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51413657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com