[英]array.splice is not a function
I am trying to remove an item from an array of unchecked with this bit of code. 我试图从未经检查的数组中删除此项代码。
function filterSearch() {
var cats = [];
$('.filter-by-type input[type="checkbox"]').change(function() {
var cat = $(this).attr('name') + ', ';
if(this.checked) {
cats += cat;
} else {
cats.splice(cats.indexOf(cat), 1);
}
console.log(cats);
});
}
filterSearch();
I am getting the error Uncaught TypeError: cats.splice is not a function
我收到错误
Uncaught TypeError: cats.splice is not a function
Basically I want to add the value to the cats[]
array if the item is checked and removed if unchecked. 基本上我想将值添加到
cats[]
数组中,如果项目被选中,如果未选中则删除。 Any help would be appreciated. 任何帮助,将不胜感激。
cats
is a array. cats
是一个阵列。 Here: 这里:
if(this.checked) {
cats += cat;
^^
} else {
cats.splice(cats.indexOf(cat), 1);
}
You are trying to concatenate an array with the +=
operator, cats
is now a string, you should use the push method instead. 您正在尝试使用
+=
运算符连接数组, cats
现在是一个字符串,您应该使用push方法。
if(this.checked) {
cats.push(cat);
} else {
cats.splice(cats.indexOf(cat), 1);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.