[英]how to remove searched item and remove that item from array?
I have a search field and in that field I can search an item. 我有一个搜索字段,在该字段中我可以搜索一个项目。 Then I have to remove it, but unfortunately I don
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
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
current id` then delete that item, but I guess it is not quite right, cause it is not working 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`然后删除该项目,但我想这是不完全正确,因为它不工作 My code
我的密码
$(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);
});
You are assiging currentRobot
您正在协助
currentRobot
currentRobot=e.target.value;
and then you are comparing its value 然后您在比较它的价值
if (robots[i].id === currentRobot) robots.splice(i, 1);
My hunch is that currentRobot
and robots[i.id
is not of the same type. 我的直觉是
currentRobot
和robots[i.id
]的类型不同。 Before the comparison try adding 在比较之前尝试添加
console.log( currentRobot, robots[i].id);
They need to be of the same data type for this comparison to work. 为了使此比较有效,它们必须具有相同的数据类型。
Also, please keep in mind that the first index of an array is 0
not 1
. 另外,请记住,数组的第一个索引是
0
而不是1
。
Without knowing what the global robots
is, we must guess. 不知道全球
robots
是什么,我们必须猜测。
robots
is a true array robots
是一个真正的数组 If robots
is a true JavaScript array, then the removal from the array should work with splice
, but: 如果
robots
是真正的JavaScript数组,则从数组中删除应该使用splice
,但是:
So, your code could deal with that as follows: 因此,您的代码可以按以下方式处理:
So replace this: 因此,替换为:
if (robots[i].id === currentRobot) robots.splice(i, 1);
..with: ..with:
if (robots[i].id === currentRobot) {
var node = document.getElementById(currentRobot);
node.parentNode.removeChild(node);
robots.splice(i, 1);
break; // and exit loop!
}
robots
is an HTML Collection robots
是HTML集合 If your global variable robots
is an HTML collection, then it is array-like, but not a true array, so you cannot apply splice
on them. 如果您的全局变量
robots
是HTML集合,则它类似于数组,但不是真正的数组,因此您无法在它们上应用splice
。 Instead you must use DOM methods. 相反,您必须使用DOM方法。
So then replace this: 因此,请替换为:
if (robots[i].id === currentRobot) robots.splice(i, 1);
..with: ..with:
if (robots[i].id === currentRobot) {
robots[i].parentNode.removeChild(roots[i]);
break; // and exit loop!
}
You need to exit the loop, as it will be awkward to continue the loop on a mutating HTML Collection, and it also is not necessary, since you expect only one match any way. 您需要退出循环,因为在变异的HTML集合上继续循环会很麻烦,这也是不必要的,因为您希望以任何方式只匹配一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.