我在使用我的jQuery過濾器覆蓋對方時遇到問題。例如,如果我想查看男士和女士,則男士會匹配項目#1並將其設置爲顯示,但將其設置爲隱藏,因爲女士不在$.inArray(filter.value, item.data("attributes")) == -1
的數據屬性中。有沒有辦法比較2個數組(一個是選擇的過濾器,另一個是item.data(「屬性」)),並顯示具有一個或多個匹配的不同項目。 (即我想男子和婦女時尚藍色或紅色)用'OR'而不是'AND'來過濾JS
$('.filter').on('click', function() {
filters = $('.filter:checked');
if (filters.length == 0)
$('.list-o-glasses').show();
else {
$('.list-o-glasses').hide();
$('.list-o-glasses').each(function(index, val) {
var item = $(this);
var show = true;
$(filters).each(function(i, filter) {
if ($.inArray(filter.value, item.data("attributes")) == -1) {
show = false;
}
});
if (show)
$('#' + $(item).attr("id")).show();
else
$('#' + $(item).attr("id")).hide();
});
}
});\
這裏呈現的HTML:
<div class="row shop-grid grid-view">
<div class="col-lg-4 col-md-6 list-o-glasses" id="BSG034BN" data-attributes="["men","wayfarer","metal","145","135","brown","brown"]" style="display: block;">
</div>
</div>
感謝,讓我知道,如果你有任何想法!
不能看到你的HTML,但如果你只是刪除其他隱藏聲明它的工作? 'else $('#'+ $(item).attr(「id」))。hide();' – andi
您的'$(filters).each('看起來不正確......您已經有'filters'定義爲jQuery對象,那麼爲什麼你再次將它傳遞給$()? – jas7457
@ jas7457 - 你是對的,我刪除了圍繞過濾器變量的$() –