我需要使用多個過濾器使用data
屬性。它在組合工作,但不能單獨工作。這裏是我的代碼:多個過濾器使用jQuery的數據屬性
<ul>
<li>
<div class="sts">
<h1>filter 1</h1>
</h1>
<div class="checkbox">
<label>
<input data-id="1" class="st" type="checkbox" />
1
</label>
</div>
<div class="checkbox">
<label>
<input data-id="2" class="st" type="checkbox" />
2
</label>
</div>
<div class="checkbox">
<label>
<input data-id="3" class="st" type="checkbox" />
3
</label>
</div>
</div>
</li>
<li>
<h1>filter 2</h1>
<div class="ats">
<div class="checkbox">
<label>
<input data-id="foo" class="at" type="checkbox" />
foo
</label>
</div>
<div class="checkbox">
<label>
<input data-id="boo" class="at" type="checkbox" />
boo
</label>
</div>
<div class="checkbox">
<label>
<input data-id="bar" class="at" type="checkbox" />
bar
</label>
</div>
</div>
</li>
<li>
<h1>filter 3</h1>
<div class="dpts">
<div class="checkbox">
<label>
<input data-id="a" class="dpt" type="checkbox" />
a
</label>
</div>
<div class="checkbox">
<label>
<input data-id="b" class="dpt" type="checkbox" />
b
</label>
</div>
<div class="checkbox">
<label>
<input data-id="c" class="dpt" type="checkbox" />
c
</label>
</div>
<div class="checkbox">
<label>
<input data-id="d" class="dpt" type="checkbox" />
d
</label>
</div>
</div>
</li>
</ul>
<ul class="list">
<li data-a="foo" data-st="1" data-dpt="a">asdw</li>
<li data-a="boo" data-st="2" data-dpt="c">qwedf</li>
<li data-a="boo" data-st="1" data-dpt="a">qwedf</li>
<li data-a="bar" data-st="3" data-dpt="b">tazxsw</li>
<li data-a="bar" data-st="1" data-dpt="b">zxcvb</li>
<li data-a="foo" data-st="1" data-dpt="b">poiuy</li>
<li data-a="boo" data-st="2" data-dpt="d">lkjhg</li>
<li data-a="boo" data-st="3" data-dpt="d">lkjhg</li>
</ul>
$(function() {
$('.at, .dpt,.st').on('click', function() {
var checkedat = $('.at:checked');
var checkeddept = $('.dpt:checked');
var checkedst = $('.st:checked');
if (checkedat.length || checkeddept.length || checkedst.length) {
if (checkeddept.length === 0) {
$('.list > li').hide();
$.each(checkedat, function() {
var prdId = $(this).attr('data-id');
$.each(checkedst, function() {
var brandId = $(this).attr('data-id');
$('.list > li[data-a="' + prdId + '"][data-st="' + brandId + '"]').show();
});
});
} else if (checkedat.length === 0) {
$('.list > li').hide();
$.each(checkedst, function() {
var brandId = $(this).attr('data-id');
$.each(checkeddept, function() {
var DeptId = $(this).attr('data-id');
$('.list > li[data-st="' + brandId + '"][data-dpt="' + DeptId + '"]').show();
});
});
} else if (checkedat.length === 0) {
$('.list > li').hide();
$.each(checkeddept, function() {
var DeptId = $(this).attr('data-id');
$.each(checkedst, function() {
var brandId = $(this).attr('data-id');
$('.list > li[data-dpt="' + DeptId + '"][data-st="' + brandId + '"]').show();
});
});
} else {
$('.list > li').hide();
$.each(checkedat, function() {
var prdId = $(this).attr('data-id');
$.each(checkedst, function() {
var brandId = $(this).attr('data-id');
$.each(checkeddept, function() {
var DeptId = $(this).attr('data-id');
$('.list > li[data-a="' + prdId + '"][data-dpt="' + DeptId + '"][data-st="' + brandId + '"]').show();
});
});
});
}
} else {
$('.list > li').show();
}
});
});
這裏是一個jsFiddle link
如果我檢查從過濾器1個&過濾器2 &過濾器3的工作,但任何選項,當我點擊從過濾器1任何購股權或過濾器2它不工作
_「但單個過濾器不起作用。」_您正在引用哪個'javascript'部分? – guest271314
@ guest271314你可以請檢查小提琴 – durai