我有一些列表和复选框作为过滤器,我需要做的是当您选择一个复选框时,将该值添加到标题旁边的小标签,并取消选中时,然后删除值。从动态数组中删除项目
我做这个工作,将每个值保存到一个数组中,当取消选中时尝试查找该值的索引并删除,但我认为我与数组中的位置有冲突。如果您逐个单击,然后逐个删除,那么它可以工作,但如果您单击它们并取消选中第一个,它将删除所有内容。有一个线索如何解决它,但不知道如何进行。
请帮忙吗? https://jsfiddle.net/panconjugo/qsgwvp63/2/
$(function() {
$(".filter-nav > li").each(function(index) {
var brands = [];
$(this).find("input:checkbox").each(function(index) {
$(this).on("click", function() {
if ($(this).is(":checked")) {
console.log("adding: " + index);
brands.push($(this).val());
console.log(brands);
$(this).parent().parent().prev().find(".selected-group").text(brands.join(", "));
} else {
console.log("removing:" + index);
brands.splice(index);
console.log(brands);
$(this).parent().parent().prev().find(".selected-group").text(brands.join(", "));
}
});
});
});
});
为什么downvote?是一个工作的例子不够? –
哎呀!是我吗? –
只有当你点击这篇文章左上角的向下箭头 –