我想創建一個系統來通過隱藏和顯示適當的項目來篩選一些標籤。jQuery顯示只匹配多個複選框參數的div
當.brandFilter被點擊時,它需要使用複選框的ID來顯示div。點擊.prodFilter時,它需要顯示相應的顏色,但不顯示任何取消選擇的ID(除非沒有被選中,在這種情況下顯示與顏色匹配的所有內容)。
現在,當我點擊Epson和惠普它的作品;但是當我點擊紅色時,它會顯示不需要的紅色Lexmark產品;當我選擇品牌時,它已經被過濾掉了。理想情況下,單擊#brnd_HP,#brnd_Epson和#typ_Red將顯示產品A和F.
取消選擇過濾器應該「撤消」以前的任何工作。
下面是標記我現在有:jQuery的並不如預期運作
<h2>Brand</h2>
<input type="checkbox" class="brandFilter" name="brandFilter" id="brnd_Canon" />
<input type="checkbox" class="brandFilter" name="brandFilter" id="brnd_Epson" />
<input type="checkbox" class="brandFilter" name="brandFilter" id="brnd_HP" />
<input type="checkbox" class="brandFilter" name="brandFilter" id="brnd_Lexmark" />
<input type="checkbox" class="brandFilter" name="brandFilter" id="brnd_Xerox" />
<h2>Color</h2>
<input type="checkbox" class="prodFilter" name="typeFilter" id="typ_Red" />
<input type="checkbox" class="prodFilter" name="typeFilter" id="typ_Blue" />
<div class="prdbx brnd_Epson typ_Red">Product A</div>
<div class="prdbx brnd_Canon typ_Red">Product B</div>
<div class="prdbx brnd_Epson typ_Blue">Product C</div>
<div class="prdbx brnd_Lexmark typ_Red">Product D</div>
<div class="prdbx brnd_Canon typ_Blue">Product E</div>
<div class="prdbx brnd_HP typ_Red">Product F</div>
,但是這是我到目前爲止所。我似乎無法用像這樣的多個參數來切換像查看類似於切換可見性的性質。惠普/愛普生部分工作正常,但一旦顏色被引入,它只是顯示與顏色ID有關的所有內容。
<script>
jQuery(document).ready(function(){
$('.brandFilter').click(function(e) {
$('.brandbx').hide();
var thisFilter = "";
$('input[name=brandFilter]:checked').each(function(e) {
thisFilter += '.'+this.id;
});
$(thisFilter).show();
});
// when a filter is clicked
$('.prodFilter').click(function(e) {
$('.prdbx').hide(); // hide all products
var thisFilter = "";
var thisCounter = 0;
// for each clicked filter
$('.prodFilter:checked').each(function(e) {
thisFilter += '.'+this.id;
$('.'+this.id).show(); // show the products matching filter
thisCounter++;
});
if(thisCounter == 0){
$('.prdbx').show(); // if no clicked filters, show all products
$('.brandbx').show();
}
});
});
</script>
嗯,當我選擇2個品牌,我沒有產品。我想這是因爲沒有一個div有兩個品牌類別;我誤解或誤解了嗎?儘管如此,顏色/品牌過濾器似乎現在工作得很好。 – ihateartists
'如果(thisCounter == 0){$ ( 'prdbx')。每個(函數(){ \t如果($(本)。是(thisFilter1)){ \t \t $(本).show( ) \t} }); $('。brandbx')。顯示(); } ' 我相信這是一個有用的修改;我相信你的原始文章,檢查品牌,然後過濾,然後取消選中過濾器將顯示所有產品;不僅僅是之前在品牌選擇中選擇的產品 – ihateartists
另一個建議:如果未設置thisFilter1,則在使用thisFilter時if檢查將始終隱藏產品。我將代碼稍微改爲:\t'if(thisFilter1!=''){if(this).is(thisFilter1)&& $(this).is(thisFilter)){$(this).show ($)(this).hide()} } else { \t if($(this).is(thisFilter)){$(this).show()} else {$(this).hide ()} }' – ihateartists