2017-07-17 94 views

回答

3

檢查這方面的工作代碼。

var getFilter = function (category) { 
 
    var filter = $("#filters ." + category + ":checked").map(function() { 
 
     return '[data-filter*="' + this.value + '"]'; 
 
    }).get().join(","); 
 
    filter = (filter.length > 0) ? filter : "*"; 
 
    return filter; 
 
} 
 

 
$("#filters :checkbox").click(function() { 
 
    var all = $(".filterme"); 
 
    var tgts = all.filter(getFilter("brand")).filter(getFilter("class")).filter(getFilter("color")); 
 
    all.not(tgts).hide(); 
 
    tgts.show(); 
 
});
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 
<p><strong>Find the right model: </strong>Select one or more checkboxes to filter your choice:</p> 
 

 

 
<table id="filters" width="620px"> 
 
    <tr> 
 
     <td width="144" align="left" valign="top"> 
 
      <input type="checkbox" class="brand" value="Audi" id="filter-Audi">Audi<br> 
 
      <input type="checkbox" class="brand" value="BMW" id="filter-BMW">BMW<br> 
 
     </td> 
 
     <td width="233" align="left" valign="top"> 
 
      <input type="checkbox" class="class" value="Compact" id="filter-Compact">Compact<br> 
 
      <input type="checkbox" class="class" value="Limousine" id="filter-Limousine">Limousine<br> 
 
      <input type="checkbox" class="class" value="SUV" id="filter-SUV">SUV<br> 
 
      <input type="checkbox" class="class" value="Sport" id="filter-Sport">Sport<br> 
 
     </td> 
 
     <td width="233" align="left" valign="top"> 
 
      <input type="checkbox" class="color" value="silver" id="filter-silver">silver<br> 
 
      <input type="checkbox" class="color" value="blue" id="filter-blue">blue<br> 
 
      <input type="checkbox" class="color" value="red" id="filter-red">red<br> 
 
      <input type="checkbox" class="color" value="white" id="filter-white">white<br> 
 
     </td> 
 
    </tr> 
 
</table> 
 

 

 
<div class="text"></div> 
 
<div id="log" style="width: 620px; line-height: 19px"></div> 
 

 
<!-- new Car 1 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="Audi Compact silver blue red"> 
 
    <strong>Audi A1</strong><br /> 
 
    Audi, Compact, silver, blue, red 
 
</div> 
 
<br /> 
 

 
<!-- new Car 2 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="BMW Compact red white"> 
 
    <strong>BMW 118i</strong><br /> 
 
    BMW Compact red white 
 
</div> 
 
<br /> 
 

 
<!-- new Car 3 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="Audi Limousine Sport silver blue red"> 
 
    <strong>Audi A4</strong><br /> 
 
    Audi Limousine Sport silver blue red 
 
</div> 
 
<br /> 
 

 
<!-- new Car 4 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="BMW SUV Sport white blue silver"> 
 
    <strong>BMW X3</strong><br /> 
 
    BMW SUV Sport white blue silver 
 
</div> 
 
<br /> 
 

 
<!-- new Car 5 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="Audi Sport Silver"> 
 
    <strong>Audi R8</strong><br /> 
 
    Audi Sport Silver 
 
</div> 
 
<br /> 
 

 
<!-- new Car 6 --> 
 

 
<div style="border-top: 1px dotted black; padding: 3px; width: 620px" class="filterme" data-filter="BMW Sport white silver blue"> 
 
    <strong>BMW Concept X</strong><br /> 
 
    BMW Sport white silver blue 
 
</div> 
 
<br />

+0

它的工作原理! Superthx! =) – Doodl

+0

如何使每個類別過濾器,因此它只顯示包含該內容是檢查 http://jsfiddle.net/ar3PY/103/ – Doodl

+0

請檢查這個小提琴http://jsfiddle.net/vks9009/13khjkey / –

0

如何顯示檢查過濾器,只爲每個類別exacly相同屬性的內容?

也看到圖像的解釋:

How to show content of checked filter that only has exacly same attribute for each category? see also image:

的jsfiddle位置: http://jsfiddle.net/ar3PY/104

<br> var getFilter = function (category) { 
var filter = $("#filters ." + category + ":checked").map(function() { 
    return '[data-filter*="' + this.value + '"]'; 
}).get().join(","); 
filter = (filter.length > 0) ? filter : "*"; 
return filter; 

}

$( 「#過濾器:複選框」)。點擊(函數(){VAR 所有= $( 「filterme」); VAR的TGT = all.filter(用getFilter( (「color」)); all.not(tgts).hide(); tgts.show(); }); filter(getFilter(「class」))