下面的功能允许用户通过 过滤产品data-attributes
,并支持同时按多个值过滤。它通过创建一个包含所选值的数组来做到这一点,当单击任何值(在本例中为选中/未选中)时,它会隐藏所有项目,然后重新显示与更新数组中的值匹配的项目。
它在过滤 one 时可以正常工作data-attribute
,但是当组合过滤多个属性时,它不再显示与任何值匹配的所有结果,而是仅显示与所有指定值匹配的结果。
我在这里发布了一个演示问题的小提琴:http: //jsfiddle.net/chayacooper/WZpMh/94/除了其中一个项目之外的所有项目都具有两者的值,因此如果其中一个过滤器是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;
});
});