我有一些列表和複選框作爲過濾器,我需要做的是當您選擇一個複選框時,將該值添加到標題旁邊的小標籤,並取消選中時,然後刪除值。從動態數組中刪除項目
我做這個工作,將每個值保存到一個數組中,當取消選中時嘗試查找該值的索引並刪除,但我認爲我與數組中的位置有衝突。如果您逐個單擊,然後逐個刪除,那麼它可以工作,但如果您單擊它們並取消選中第一個,它將刪除所有內容。有一個線索如何解決它,但不知道如何進行。
請幫忙嗎? 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?是一個工作的例子不夠? –
哎呀!是我嗎? –
只有當你點擊這篇文章左上角的向下箭頭 –