【问题标题】:Show all divs on Unchecking the checkboxes在取消选中复选框时显示所有 div
【发布时间】:2014-05-23 20:11:02
【问题描述】:

我根据不同的数据属性(如数据品牌、数据存储等)在不同的 div 中有一个产品列表代码。我正在使用我朋友帮助我开发的复选框选择过滤记录。我需要对此进行一些小改动。查看代码。

<html>
  <head>
    <meta charset="utf-8">
    <title>JS Bin</title>
  </head>
  <body>
    <div>
      <div class="content" 
           data-category="shoes" 
           data-price="4000" 
           data-size="38" 
           data-brand="Nike">
        <img src="http://placehold.it/120x80">
        <p>Nike 38<br>$4000</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="6000" 
           data-size="20"
           data-brand="Nike">
        <img src="http://placehold.it/140x80">
        <p>Nike 20<br>$6000</p>
      </div>    
      <div class="content" 
           data-category="shoes" 
           data-price="500" 
           data-size="20"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 20<br>$500</p>
      </div>  
      <div class="content" 
           data-category="shoes" 
           data-price="780" 
           data-size="42"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 42<br>$780</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="1200" 
           data-size="40"
           data-brand="Sunbaby">
        <img src="http://placehold.it/140x80">
        <p>Sunbaby 40<br>$1200</p>
      </div>
      <div class="content" 
           data-category="shoes" 
           data-price="2000" 
           data-size="70"
           data-brand="Andrew">
        <img src="http://placehold.it/120x80">
        <p>Andrew 70<br>$2000</p>
      </div>
      <div class="content" 
           data-category="shoes" 
           data-price="800" 
           data-size="50"
           data-brand="Sunbaby">
        <img src="http://placehold.it/120x80">
        <p>Sunbaby 50<br>$800</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="1300"
           data-size="20"
           data-brand="Nike">
        <img src="http://placehold.it/140x80">
        <p>Nike 20<br>$1300</p>
      </div>
      <div class="content" 
           data-category="shirts" 
           data-price="800" 
           data-size="35"
           data-brand="Andrew">
        <img src="http://placehold.it/140x80">
        <p>Andrew 35<br>$800</p>
      </div>
    </div>
    <form id="filter">
      <div>
        <input type="checkbox" 
               name="brand" 
               value="Andrew" checked>               
               Andrew
        </input>
        <input type="checkbox" 
               name="brand" 
               value="Sunbaby">
               Sunbaby
        </input>
        <input type="checkbox" 
               name="brand" 
               value="Nike">
               Nike
        </input>
      </div>
      <div>
        <input type="checkbox" 
               name="category" 
               value="shoes" checked>               
               Shoes
        </input>
        <input type="checkbox" 
               name="category" 
               value="shirts">
               Shirts
        </input>
      </div>
      <div>
        <input type="radio" 
               name="price" 
               value="0-9000"         
               checked>
               All
        </input>
        <input type="radio" 
               name="price" 
               value="0-999">
               $0-$1000
        </input>
        <input type="radio" 
               name="price" 
               value="1000-2000">
               $1000-$2000
        </input>
      <div> 
        <div>
        <input type="radio" 
               name="size" 
               value="0-80"         
               checked>
               All
        </input>
        <input type="radio" 
               name="size" 
               value="0-25">
               Small
        </input>
        <input type="radio" 
               name="size" 
               value="26-45">
               Medium
        </input>
        <input type="radio" 
               name="size" 
               value="46-80">
               Big
        </input>
      <div> 
    </form>
  </body>
</html>

css

.hidden {display: none;}

.content {border-radius: 5px; border: 1px solid #bbb;padding: 5px; margin: 5px; float: left;}
#filter {clear: left;}

脚本

var filterContentForm = function(content, form){  
  var filter = function() {  
    var checkBoxGroups = {},
        radioGroups = {};
    var addRadioGroup = function(name){
      radioGroups[name] = {      
        el: $('input[name='+name+']:checked')
      };
      var n = radioGroups[name];
      n.el
      .each(function(){
        n.range = $(this).val().split('-');
        n.from = Number(n.range[0]);
        n.to = Number(n.range[1]);      
      });
    };      
    $('#filter input[type=radio]')
    .each(function(){
      addRadioGroup($(this).attr('name'));
    });      
    var addCheckBoxGroup = function(name){
      checkBoxGroups[name] = {
        el: $('input[name='+name+']:checked'),      
        ch: []
      };
      var n = checkBoxGroups[name];
      n.el.each(function(){
        n.ch.push($(this).val());
      });
    };
    $('#filter input[type=checkbox]')
    .each(function(){
      addCheckBoxGroup($(this).attr('name'));
    });
    var contents = $(content), all = 0;
    contents.removeClass('hidden')
    .each(function(){
      var $this = $(this),
          price = $this.data('price');
      for(var c in radioGroups){ 
        var n = radioGroups[c],
            d = Number($this.data(c));
        if(d < n.from || d > n.to){
          $this.addClass('hidden');
          all++;
          return;
        }
      }    
      var show = 0, i;
      for(var c in checkBoxGroups){   
        var n = checkBoxGroups[c], 
            d = $this.data(c);      
        for(i = 0; i < n.ch.length; i++){        
          if(d === n.ch[i]) {
            show++; break;
          }
        } 
      }
      var l = Object.keys(checkBoxGroups).length;
      if(show < l) {
        $this.addClass('hidden');
        all++;
      }
    });
    if(all > contents.length - 1) 
      contents.removeClass('hidden');
  };
  $(form+' input').change(filter);
  filter();
};
filterContentForm('.content', '#filter'); 
#filter {clear: left;}

上面的代码运行良好。我只需要对此进行一点小改动。也就是说,在开始时检查两个复选框,即品牌即耐克和类别即鞋子。我只是希望一开始,这两个复选框也需要取消选中,所有记录都可见,但是当我从 Andrew and Shoes 复选框中删除“选中”时,过滤不会发生。

请指导我如何在开始时不选中所有复选框,然后在选择复选框后进行过滤。 感谢您的帮助

【问题讨论】:

标签: javascript jquery html css


【解决方案1】:

您的过滤器代码似乎有点错误。 确保在标题中添加 jquery js!

要切换复选框和/或单选按钮的选中/未选中状态,只需从标签中添加/删除选中属性

<input type="checkbox" 
           name="category" 
           value="shoes" **checked**> 

【讨论】:

  • @Varun-当我尝试使用您的逻辑时,即仅将 checked 与鞋子复选框放在一起。过滤器有效。但是当我同时保持 checked鞋子和安德鲁 checkbox.filter 不起作用。如何使过滤器工作保持所有复选框未选中....
  • 刚刚在 Anton 上面评论中给出的 jsfiddle 链接上检查了它。这种组合运行良好。当根本没有检查“品牌”或“类别”中的任何一个时,就会出现问题。
  • 我只签了那个。你能检查一次吗
  • 一旦我有时间调查,我会向您更新确切的问题。可能再过几个小时!
【解决方案2】:

好的,这就是问题所在:

您正在检查以下条件

if(show &lt; l)

show 是检查的 filterCategories [在您的情况下为:brand and category] 的计数。正在与 l 进行比较,后者是当前 filterCategories 的总数。

所以,当只选中一个类别时,这个条件就成立了,下面的代码

   `$this.addClass('hidden');
    all++;`

被执行。这使您的 all++ 达到 contents.length 的值,因此您的以下代码将被执行

  if(all > contents.length - 1) 
  contents.removeClass('hidden');

它会覆盖过滤器并显示所有项目 [在您的情况下为 div]

要解决此问题,请参阅以下代码。 activeFilterCount 变量是所需的更改。只需将您的代码从 var show=0,i; 替换为 all++;} 即可:

var show = 0, i, activeFilterCount=0; for(var c in checkBoxGroups){ var n = checkBoxGroups[c], d = $this.data(c); if(n.ch.length > 0){ activeFilterCount++; } for(i = 0; i < n.ch.length; i++){
if(d === n.ch[i]) { show++; break; } } } if(show < activeFilterCount) { $this.addClass('hidden'); all++; }

我知道它有点太长了,但我希望它对你有帮助!如果有什么不清楚的地方请告诉我。

【讨论】:

  • @Varun-似乎代码无法在我的本地 pc 上运行。但在 fiddle 上运行。任何线索先生????
  • 不看文件就很难猜到。你能详细说明什么不起作用吗?一个问题可能是缓存。清除缓存并尝试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
相关资源
最近更新 更多