我想创建一个系统来通过隐藏和显示适当的项目来筛选一些标签。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