繁体   English   中英

如何删除搜索到的项目并从数组中删除该项目?

[英]how to remove searched item and remove that item from array?

我有一个搜索字段,在该字段中我可以搜索一个项目。 然后,我必须将其删除,但不幸的是,我不t know how I can remove that item. Please, could you help me with that. I tried the following: If t know how I can remove that item. Please, could you help me with that. I tried the following: If t know how I can remove that item. Please, could you help me with that. I tried the following: If ID of currentRobot is equal to当前id`然后删除该项目,但我想这是不完全正确,因为它不工作 在此处输入图片说明 我的密码

$(document).ready(function(){
    var currentRobot=null;

    for( var i=0; i<robots.length; i++){

        var robot = document.createElement('div');

        robotsContainer.appendChild(robot);

        robot.className='whole-block';  
        robot.setAttribute('id', robots[i].id);


        robot.innerHTML += "<img src='" + robots[i].avatar + "'>";
        robot.innerHTML += "<h3>" +robots[i].first_name + "</h3>";
        robot.innerHTML += "<h3>" +robots[i].last_name + "</h3>";
        robot.innerHTML += "<p>" +robots[i].email + "</p>";
        robot.innerHTML += "<p>" +robots[i].gender + "</p>";
        robot.innerHTML += "<p>" +robots[i].friends + "</p>";

    }   
        console.log(value)
    value.onkeyup = function(e){
        currentRobot=e.target.value;
        var robots = document.querySelectorAll("div[id]:not([id='"+ this.value +"']):not([id='robotsContainer'])");
        for(var i = 0; i < robots.length; i++ ){
            if(this.value==""){ 
                robots[i].style.display="block";
            } else {
                robots[i].style.display="none";
            }
        };

    };


 var button= document.getElementById('button');  
        button.addEventListener('click', function(e){
             e.preventDefault(); 

        for(var i = 0; i < robots.length; i++){
                if (robots[i].id ===  currentRobot) robots.splice(i, 1);

        }
        }, false);

});

您正在协助currentRobot

currentRobot=e.target.value;

然后您在比较它的价值

  if (robots[i].id ===  currentRobot) robots.splice(i, 1);

我的直觉是currentRobotrobots[i.id ]的类型不同。 在比较之前尝试添加

console.log( currentRobot, robots[i].id);

为了使此比较有效,它们必须具有相同的数据类型。

另外,请记住,数组的第一个索引是0而不是1

不知道全球robots是什么,我们必须猜测。

1. robots是一个真正的数组

如果robots是真正的JavaScript数组,则从数组中删除应该使用splice ,但是:

  • 您最好立即退出循环
  • 此删除操作对您的页面无效

因此,您的代码可以按以下方式处理:

因此,替换为:

if (robots[i].id === currentRobot) robots.splice(i, 1);

..with:

if (robots[i].id === currentRobot) {
    var node = document.getElementById(currentRobot);
    node.parentNode.removeChild(node);
    robots.splice(i, 1);
    break; // and exit loop!
}

2. robots是HTML集合

如果您的全局变量robots是HTML集合,则它类似于数组,但不是真正的数组,因此您无法在它们上应用splice 相反,您必须使用DOM方法。

因此,请替换为:

if (robots[i].id === currentRobot) robots.splice(i, 1);

..with:

if (robots[i].id === currentRobot) {
    robots[i].parentNode.removeChild(roots[i]);
    break; // and exit loop!
}

您需要退出循环,因为在变异的HTML集合上继续循环会很麻烦,这也是不必要的,因为您希望以任何方式只匹配一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM