以下の関数を使用すると、ユーザーは製品をdata-attributes
でフィルタリングでき、複数の値で同時にフィルタリングできます。これは、選択された値の配列を作成することによって行われます。いずれかの値がクリックされると(この場合はチェック/オフ)、すべてのアイテムが非表示になり、更新された配列の値に一致するアイテムが再表示されます。
1つをフィルタリングする場合は正しく機能しますdata-attribute
が、複数の属性でフィルタリングするように組み合わせると、いずれかの値に一致するすべての結果が表示されなくなり、代わりに、指定されたすべての値に一致する結果のみが表示されます。
ここに問題を示すフィドルを投稿しました:http://jsfiddle.net/chayacooper/WZpMh/94/ 1つを除くすべてのアイテムは両方の値を持っているdata-style="V-Neck"
ためdata-color="Black"
、いずれかのフィルターが選択されていますが、別の値の場合、data-attribute
一部のアイテムは非表示になっています。
$(document).ready(function () {
var selected = [];
$('#attributes-Colors *').click(function () {
var attrColor = $(this).data('color');
var $this = $(this);
if ($this.parent().hasClass("active")) {
$this.parent().removeClass("active");
selected.splice(selected.indexOf(attrColor),1);
}
else {
$this.parent().addClass("active");
selected.push(attrColor);
}
$("#content").find("*").hide();
$.each(selected, function(index,item) {
$('#content').find('[data-color *="' + item + '"]').show();
});
return false;
});
$('#attributes-Silhouettes *').click(function () {
var attrStyle = $(this).data('style');
var $this = $(this);
if ($this.parent().hasClass("active")) {
$this.parent().removeClass("active");
selected.splice(selected.indexOf(attrStyle),1);
}
else {
$this.parent().addClass("active");
selected.push(attrStyle);
}
$("#content").find("*").hide();
$.each(selected, function(index,item) {
$('#content').find('[data-style *="' + item + '"]').show();
});
return false;
});
});