我制作了具有多个选项的过滤器。如您所见,有时没有结果。我说:“没有结果。”作为用户反馈,将其反馈给访问者。
现在,我想向访问者显示过滤器选项中已有的结果数量。例如:显示全部(10)或教育(1)。这样,用户便知道了自己的期望,而不会选择空的过滤器。
但是有些事情使它变得非常复杂:数字可以通过某些组合来改变。例如:如果选择过滤器一,则过滤器二的选项更少。等
有人可以帮助我吗?
$("select.filterby").change(function() {
var filters = $.map($("select.filterby").toArray(), function(e) {
return $(e).val();
}).join(".");
$("div#FilterContainer").find("div.job-offer-card").hide();
if ($("div#FilterContainer").find("div.job-offer-card." + filters).length) {
$("div#FilterContainer").find("div.job-offer-card." + filters).show();
} else {
$("div#FilterContainer").find("div.noresults").show();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span> View by:</span>
<label for="location">Location</label>
<select id="location" class="filterby">
<option value="all">Show All</option>
<option value="australia">Australia</option>
<option value="singapore">Singapore</option>
<option value="newzealand">New Zealand</option>
<option value="usa">USA</option>
<option value="uk">UK</option>
<option value="vietnam">Vietnam</option>
<option value="global">Global</option>
<option value="othercountry">Other</option>
</select>
<label for="type">Type</label>
<select id="type" class="filterby">
<option value="all">Show All</option>
<option value="accommodation">Accommodation</option>
<option value="app">App/Technology</option>
<option value="business">Business</option>
<option value="health">Health</option>
<option value="hospitality">Hospitality</option>
<option value="education">Education</option>
<option value="retail">Retail</option>
<option value="services">Services</option>
<option value="travel">Travel</option>
</select>
<label for="code">Code</label>
<select id="code" class="filterby">
<option value="all">Show All</option>
<option value="001">001</option>
<option value="002">002</option>
<option value="003">003</option>
<option value="004">004</option>
</select>
<div id="FilterContainer">
<div class="job-offer-card noresults">
<p>no results</p>
</div>
<div class="job-offer-card all vietnam accommodation travel 004">
<p>1 all vietnam accommodation travel</p>
</div>
<div class="job-offer-card all vietnam travel 003">
<p>2 all vietnam travel</p>
</div>
<div class="job-offer-card all vietnam accommodation travel">
<p>3 all vietnam accommodation travel</p>
</div>
<div class="job-offer-card all australia accommodation travel 003">
<p>4 all australia accommodation travel</p>
</div>
<div class="job-offer-card all australia education">
<p>5 all australia</p>
</div>
<div class="job-offer-card all vietnam newzealand usa accommodation">
<p>6 all vietnam accommodation</p>
</div>
<div class="job-offer-card all vietnam accommodation travel">
<p>7 all vietnam accommodation travel</p>
</div>
<div class="job-offer-card all vietnam travel">
<p>8 all vietnam travel</p>
</div>
<div class="job-offer-card all vietnam accommodation travel">
<p>9 all vietnam accommodation travel</p>
</div>
<div class="job-offer-card all australia accommodation travel">
<p>10 all australia accommodation travel</p>
</div>
<div class="job-offer-card all australia health 001">
<p>11 all australia</p>
</div>
<div class="job-offer-card all vietnam accommodation 002">
<p>12 all vietnam accommodation</p>
</div>
</div>
答案 0 :(得分:0)
任何jquery对象的length
属性将包含元素数量。您已经使用过了。
如果要将数字添加到每个选项,则可以循环浏览所有选项并计算每种情况。
$("select.filterby option").each(function() {
var t=$(this),
val=t.val(),
text=t.text(),
count=$("#FilterContainer").find("div.job-offer-card."+val).length;
v.text(text+" ("+val+")");
});