下面的功能允许用户通过 过滤产品data-attributes
,并支持同时按多个值过滤。如何on load
在每个选项旁边显示潜在结果的数量?理想情况下,我还想显示他们运行的每个搜索的结果总数。
我在这里发布了一个函数的工作示例:http: //jsfiddle.net/chayacooper/hSwcr/1/
var selected = { color: [], style: [] };
$('.filterOptions').on("change", ":checkbox", function (e) {
var type = $(e.delegateTarget).data("type");
var $this = $(this);
var attrValue = $this.data(type);
if ($this.parent().hasClass("active")) {
$this.parent().removeClass("active");
selected[type].splice(selected[type].indexOf(attrValue),1);
}
else {
$this.parent().addClass("active");
selected[type].push(attrValue);
}
var checked = $(":checked", ".filterOptions");
// show all of the available options if...
if (checked.length == 0 || checked.filter(".all").length > 0) {
$('#content').find('*').show();
}
else {
$("#content").find("*").hide();
for (var key in selected) {
$.each(selected[key], function(index,item) {
$('#content').find('[data-' + key + ' *="' + item + '"]').show();
});
}
}
});