[英]When I click checkbox on a list of items and delete them not all of them are removed, but when I only check off one it works fine. Why?
When I click a checkbox and click delete on a single item in the list it deletes as expected.当我单击复选框并单击列表中单个项目的删除时,它会按预期删除。 When I check all items and click delete it does not.
当我检查所有项目并单击删除时,它不会。 I do not know why and I would like to know.
我不知道为什么,我想知道。 I understand that in order to remove elements in the array in reference to a DOM element index you must first get the DOM elements index, but I am not interested in that at the moment.
我知道为了删除数组中引用 DOM 元素索引的元素,您必须首先获取 DOM 元素索引,但目前我对此不感兴趣。 I really just have no idea why this code doesn't work and it makes me feel stupid.
我真的不知道为什么这段代码不起作用,这让我觉得很愚蠢。
window.onload = function() { let toDos = [ { title: "MOM", complete: false } ,{ title: "DAD", complete: false } ,{ title: "The Universe", complete: false } ,{ title: "MOM", complete: false } ,{ title: "DAD", complete: false } ,{ title: "The Universe", complete: false} ]; const deleteButton = document.getElementsByTagName("button")[0]; var inputElems = document.querySelectorAll(".todo-delete"); deleteButton.addEventListener("click", function() { inputElems = document.querySelectorAll(".todo-delete"); for (var i = 0; i < inputElems.length; i += 1) { if (inputElems[i].checked) { toDos = toDos.filter(function(val, index) { if (index !== i) { return val } }); } } inputElems = document.querySelectorAll(".todo-delete"); console.log(toDos); renderDOM(toDos); }); function renderDOM(toDos) { const todoList = document.getElementById('todo-list'); todoList.textContent = ''; toDos.forEach(function(toDo) { const newLi = document.createElement('li'); const checkbox = document.createElement('input'); checkbox.className = "todo-delete"; checkbox.type = "checkbox"; newLi.textContent = toDo.title; todoList.appendChild(newLi); newLi.appendChild(checkbox); }); } renderDOM(toDos) };
<ul id="todo-list"> </ul> <hr> <button>delete</button>
The problem is when you filter toDos
array.问题是当您过滤
toDos
数组时。 Your for loop index i
becomes more than there are elements in toDos
array after you filter more than half of toDos
content in one operation.在一次操作中过滤了一半以上的
toDos
内容后,您的 for 循环索引i
变得比toDos
数组中的元素多。
Change for (var i = 0; i < inputElems.length; i += 1) {
to for (var i = inputElems.length -1; i >= 0; i--)
将
for (var i = 0; i < inputElems.length; i += 1) {
改为for (var i = inputElems.length -1; i >= 0; i--)
Here we change the loop directiom from going left to right we are going right to left, meaning decreasing index each iteration, so when we filter out 1 element we also decrease the i
.在这里,我们将循环方向从左到右更改为从右到左,这意味着每次迭代都会减少索引,因此当我们过滤掉 1 个元素时,我们也会减少
i
。
The problem is that you filter your array after each delete and the indexes get changed.问题是您在每次删除后过滤数组并且索引被更改。 You need to know all the indexes to delete before you actually delete them.
在实际删除它们之前,您需要知道要删除的所有索引。
window.onload = function() {
let
toDos = [ {
title : "MOM",
complete : false
}, {
title : "DAD",
complete : false
}, {
title : "The Universe",
complete : false
}, {
title : "MOM",
complete : false
}, {
title : "DAD",
complete : false
}, {
title : "The Universe",
complete : false
} ];
const deleteButton = document.getElementsByTagName("button")[0];
var inputElems = document.querySelectorAll(".todo-delete");
deleteButton.addEventListener("click", function() {
inputElems = document.querySelectorAll(".todo-delete");
var removes = []; // added this part
for (var i = 0; i < inputElems.length; i += 1) {
if (inputElems[i].checked) {
removes.push(i); // added this part
}
}
toDos = toDos.filter(function(val, index) {
if (removes.indexOf(index) === -1) { // added this part
return val;
}
});
inputElems = document.querySelectorAll(".todo-delete");
renderDOM(toDos);
});
function renderDOM(toDos) {
const
todoList = document.getElementById('todo-list');
todoList.textContent = '';
toDos.forEach(function(toDo) {
const
newLi = document.createElement('li');
const
checkbox = document.createElement('input');
checkbox.className = "todo-delete";
checkbox.type = "checkbox";
newLi.textContent = toDo.title;
todoList.appendChild(newLi);
newLi.appendChild(checkbox);
});
}
renderDOM(toDos)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.