2016-12-16 128 views
2

我有一些列表和複選框作爲過濾器,我需要做的是當您選擇一個複選框時,將該值添加到標題旁邊的小標籤,並取消選中時,然後刪除值。從動態數組中刪除項目

我做這個工作,將每個值保存到一個數組中,當取消選中時嘗試查找該值的索引並刪除,但我認爲我與數組中的位置有衝突。如果您逐個單擊,然後逐個刪除,那麼它可以工作,但如果您單擊它們並取消選中第一個,它將刪除所有內容。有一個線索如何解決它,但不知道如何進行。

請幫忙嗎? 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(", ")); 
     } 
     }); 
    }); 
    }); 
}); 

回答

1

相反維持由添加/移除項的數組的,這是非常非常簡單鉤到change事件的複選框的,然後建立利用map()一個新的數組,它包含所選擇的數據該組中的項目。然後,您可以將它們結合在一起,並根據需要顯示文本。試試這個:

$('.filter-nav input:checkbox').on("change", function() { 
 
    var $parent = $(this).closest('.filter-nav > li'); 
 
    var items = $parent.find('input:checkbox:checked').map(function() { 
 
    \t return this.value; 
 
    }).get(); 
 
    $parent.find('.selected-group').text(items.join(', ')); 
 
});
.filter-nav { 
 
    list-style: none; 
 
    padding: 0; 
 
} 
 
.filter-nav li { 
 
    line-height: 40px; 
 
} 
 
.filter-nav > li > h3 { 
 
    background-color: #222; 
 
    color: white; 
 
    padding-left: 20px; 
 
    cursor: pointer; 
 
} 
 
.selected-group { 
 
    color: yellow; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul class="filter-nav"> 
 
    <li> 
 
    <h3>Brand <small class="selected-group"></small></h3> 
 
    <ul> 
 
     <li> 
 
     <label for="brand1">Brand1</label> 
 
     <input type="checkbox" id="brand1" value="Brand 1"> 
 
     </li> 
 
     <li> 
 
     <label for="brand1">Brand2</label> 
 
     <input type="checkbox" id="brand2" value="Brand 2"> 
 
     </li> 
 
     <li> 
 
     <label for="brand1">Brand3</label> 
 
     <input type="checkbox" id="brand3" value="Brand 3"> 
 
     </li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <h3>Size <small class="selected-group"></small></h3> 
 
    <ul> 
 
     <li> 
 
     <label for="xs">XS</label> 
 
     <input type="checkbox" id="xs" value="xs"> 
 
     </li> 
 
     <li> 
 
     <label for="m">M</label> 
 
     <input type="checkbox" id="m" value="m"> 
 
     </li> 
 
     <li> 
 
     <label for="l">L</label> 
 
     <input type="checkbox" id="l" value="l"> 
 
     </li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <h3>Color <small class="selected-group"></small></h3> 
 
    <ul> 
 
     <li> 
 
     <label for="blue">Blue</label> 
 
     <input type="checkbox" id="blue" value="blue"> 
 
     </li> 
 
     <li> 
 
     <label for="green">Green</label> 
 
     <input type="checkbox" id="green" value="green"> 
 
     </li> 
 
     <li> 
 
     <label for="red">Red</label> 
 
     <input type="checkbox" id="red" value="red"> 
 
     </li> 
 
    </ul> 
 
    </li> 
 
</ul>

+0

爲什麼downvote?是一個工作的例子不夠? –

+0

哎呀!是我嗎? –

+0

只有當你點擊這篇文章左上角的向下箭頭 –

3

更改代碼brands.splice(index);brands.splice(brands.indexOf($(this).val()), 1);

首先你要找到數組項目索引,然後使用該索引從數組中刪除