简体   繁体   English

当我单击项目列表上的复选框并删除它们时,并非所有项目都被删除,但是当我只勾选一个时,它工作正常。 为什么?

[英]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.

相关问题 如何在删除其中一个项目时重新编号? - How to renumber items when one of them is removed? 当我点击其中一个元素时,所有元素都会消失,但只需要我没有点击的元素消失 - All the elements disappear when I click on one of them, but only need the elements that I haven't clicked on to disappear 当我第一次点击删除按钮时,所有的笔记都消失了,但是当我刷新页面时它工作得很好 - When I first click the delete button, all the notes are disappeared but, when I refresh the page it works just fine 在跨度Javascript上单击它们时,这些项不会消失 - items will not disapear when I click them on the span Javascript 为什么当我检查循环中的字符串是否包含在字符串数组中时,所有字符串都为假? - Why when I check to see if a string in a loop is included in an array of strings do I get false for all of them? 单击时选择多个元素,现在只需要其中一个(单击功能类似于浏览器中的检查器) - I select multiple Elements when I click, now I want only one of them (click function similar to the inspector from the browser) 单击其中之一时如何切换两个图像? - how to toggle two image when I click one of them? 当我单击它一次时,此javascript函数工作正常,但当我单击两次时不起作用 - This javascript function works fine when i click it one, but does not work when i click it twice Javascript选中/取消选中所有复选框功能仅在存在多个复选框时才有效 - Javascript check/uncheck all checkbox function only works when there is more than one checkbox 当我点击全选复选框时为什么计数错误 - why counting wrong when i click select all checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM