简体   繁体   English

使用复选框按类过滤div

[英]Filtering divs by classes with checkboxes

I'm currently working on filtering a pile of divs with checkboxes. 我目前正在使用复选框过滤一堆div。

<ul id="filters">
    <li>
        <table id="filters_opt" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
        <td><input type="checkbox" checked value="Familien" id="filter-Familien" />
        <label for="filter-Familien">Familien/Kinder</label></td>

        <td><input type="checkbox" checked value="Jugend" id="filter-Jugend" />
        <label for="filter-Jugend">Jugend</label></td>

        <td><input type="checkbox" checked value="Senioren" id="filter-Senioren" />
        <label for="filter-Senioren">Senioren</label></td>

        <td><input type="checkbox" checked value="Musik" id="filter-Musik" />
        <label for="filter-Musik">Musik</label></td>
        <td rowspan="3" align="center" id="filter_anwenden_big"><input type="button" class="send_button" value="Filter anwenden"></td>
       <tr>
</table>   
    </li>
</ul>



$("#filters :checkbox:checked").each(function() {

   $("." + $(this).val()).show();

});

so far this is showing all divs with the classes selected by the checkboxes. 到目前为止,这显示的是所有具有复选框选中的类的div。 But I can't seem to wrap my head around the following: 但是我似乎无法绕过以下话题:

When I have these conditions: class A class B or a combination ie classes A + B + C 当我有以下条件时:A类B类或组合,即A + B + C类

Shouldn't the results get smaller? 结果不应该变小吗? It seems to always display all divs from class A, all divs from class B and all divs from class C. But I'd like to display results like only divs that contain class A + B. And not A and B seperately. 它似乎总是显示A类的所有div,B类的所有div和C类的所有div。但是我想显示的结果仅像包含A + B的div一样,而不分别显示A和B。

Any ideas my fellow coders? 我的编码员有什么想法吗?

JsFiddle! 的jsfiddle! - Here's a working example. -这是一个有效的例子。 If I check on "Erwachsene" and "Flingern" it shouldn't show the "only Flingern" example nor the "Senioren - Flingern - Duesseltal" example. 如果我选中“ Erwachsene”和“ Flingern”,则不应显示“仅Flingern”示例,也不应显示“ Senioren-Flingern-Duesseltal”示例。

What I'm trying to achieve is that the more checkboxes I activate, the more results will be narrowed down until only few are left. 我想要实现的是,我激活的复选框越多,将缩小更多的结果,直到只剩下几个。

With my current code it just shows too much results. 用我当前的代码,它只会显示太多结果。 Because it shows all divs with the class "Flingern" and all with the class "Erwachsene". 因为它显示所有“ Flingern”类和所有“ Erwachsene”类的div。

Hope this makes it clearer. 希望这使事情变得更清楚。

You can change your selector query like this : 您可以像这样更改选择器查询:

    $(".send_button").click(function() {

      $(".filter").hide();

      var classes = ['.filter'];

      $("#filters :checkbox:checked").each(function() {
               classes.push($(this).val());
           });

      $(classes.join('.')).show();

      return false;
   });

Updated Fiddle : https://jsfiddle.net/d621ageL/1/ 更新的小提琴: https : //jsfiddle.net/d621ageL/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM