好的...所以我回到了发布此问题之前一直在寻找的类似问题。您可以看到该问题/解决方案HERE。在发布这个问题之前,我应该花更多时间了解这一点。
这是一个可行的解决方案
简而言之,我正在使用的解决方案通过复选框组的名称属性循环(Pho3nixHun 在我的参考问题中的解决方案)
它开始显示整个指南列表。然后,它将过滤到任何和所有选择的运动,然后将使用任何和所有选择的状态过滤这些结果。
这里是完整的JSFiddle
还有工作的 jQuery:
var bySport = [], byState = [];
$("input[name=sport]").on( "change", function() {
if (this.checked) bySport.push("[data-category~='" + $(this).attr("value") + "']");
else removeA(bySport, "[data-category~='" + $(this).attr("value") + "']");
});
$("input[name=state]").on( "change", function() {
if (this.checked) byState.push("[data-category~='" + $(this).attr("value") + "']");
else removeA(byState, "[data-category~='" + $(this).attr("value") + "']");
});
$("input").on( "change", function() {
var str = "Include items \n";
var selector = '', cselector = '', nselector = '';
var $lis = $('.guide-list > div'),
$checked = $('input:checked');
if ($checked.length) {
if (bySport.length) {
if (str == "Include items \n") {
str += " " + "with (" + bySport.join(',') + ")\n";
$($('input[name=sport]:checked')).each(function(index, bySport){
if(selector === '') {
selector += "[data-category~='" + bySport.id + "']";
} else {
selector += ",[data-category~='" + bySport.id + "']";
}
});
} else {
str += " AND " + "with (" + bySport.join(' OR ') + ")\n";
$($('input[name=sport]:checked')).each(function(index, bySport){
selector += "[data-category~='" + bySport.id + "']";
});
}
}
if (byState.length) {
if (str == "Include items \n") {
str += " " + "with (" + byState.join(' OR ') + ")\n";
$($('input[name=state]:checked')).each(function(index, byState){
if(selector === '') {
selector += "[data-category~='" + byState.id + "']";
} else {
selector += ",[data-category~='" + byState.id + "']";
}
});
} else {
str += " AND " + "with (" + byState.join(' OR ') + ")\n";
$($('input[name=state]:checked')).each(function(index, byState){
if(cselector === '') {
cselector += "[data-category~='" + byState.id + "']";
} else {
cselector += ",[data-category~='" + byState.id + "']";
}
});
}
}
$lis.hide();
console.log(selector);
console.log(cselector);
console.log(nselector);
if (cselector === '' && nselector === '') {
$('.guide-list > div').filter(selector).show();
} else if (cselector === '') {
$('.guide-list > div').filter(selector).filter(nselector).show();
} else if (nselector === '') {
$('.guide-list > div').filter(selector).filter(cselector).show();
} else {
$('.guide-list > div').filter(selector).filter(cselector).filter(nselector).show();
}
} else {
$lis.show();
}
$("#result").html(str);
});
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
还有 HTML:
<pre id=result> </pre> <!-- PRE simply shows filters in action in the results to double check everything's working. Can remove -->
<h2>Let's Find You a Guide</h2>
<div class="filter-wrap">
<h3><strong>Select any or all activities you're interested in:</strong></h3>
<ul>
<li><label><input type="checkbox" name="sport" value="fish" id="fish" /> Fishing</label></li>
<li><label><input type="checkbox" name="sport" value="hunt" id="hunt" /> Hunting</label><br>
<li><label><input type="checkbox" name="sport" value="charter" id="charter" /> Boat Charters</label></li>
<li><label><input type="checkbox" name="sport" value="hike" id="hike" /> Hiking</label></li>
<li><label><input type="checkbox" name="sport" value="raft" id="raft" /> Rafting</label></li>
<li><label><input type="checkbox" name="sport" value="other" id="other" /> Other</label></li>
</ul>
<h3><strong>Select any states you're willing to travel to:</strong></h3>
<ul>
<li><label><input type="checkbox" name="state" value="AK" id="AK" /> Alaska</label></li>
<li><label><input type="checkbox" name="state" value="CA" id="CA" /> California</label></li>
<li><label><input type="checkbox" name="state" value="CO" id="CO" /> Colorado</label></li>
<li><label><input type="checkbox" name="state" value="NY" id="NY" /> New York</label></li>
</ul>
</div>
<hr/>
<div class="guide-list">
<div class="guide" data-category="raft CA CO">Rafting in California or Colorado</div>
<div class="guide" data-category="hike raft CO">Colorado Hiking and Rafting</div>
<div class="guide" data-category="raft NY ">Upstate New York Rafting</div>
<div class="guide" data-category="raft hunt NY">NY Hunting and Rafting Outfitters</div>
<div class="guide" data-category="raft NY">NYC Rafting Co.</div>
<div class="guide" data-category="charter NY">Charter Boats of NYC</div>
<div class="guide" data-category="hike raft other CO">Colorado Hike Bike and Raft</div>
<div class="guide" data-category="charter other NY">Bike and Boat Rentals New York</div>
<div class="guide" data-category="fish CO">Colorado Fishing Tours</div>
<div class="guide" data-category="fish CA">Fishing Guides San Francisco</div>
<div class="guide" data-category="hike other CA">California Hiking and Other Stuff</div>
<div class="guide" data-category="raft AK">Alaskan Rafting Adventures</div>
<div class="guide" data-category="raft hunt CO">Denver Raft and Hunt</div>
</div>