简体   繁体   English

使用 JQuery 按带有复选框的数据属性过滤 div 内容

[英]Filter div Content by data-attribute with Checkbox using JQuery

I have a list of cards inside a div with class name .cards-list and I want to to filter those cards by data-attribute using checkbox我在div中有一个卡片列表,其中包含 class 名称.cards-list ,我想使用复选框按数据属性过滤这些卡片

I tried the following jQuery code but it is not working, after checking checkbox all my cards are hidden and not showing again when I uncheck it我尝试了以下 jQuery 代码,但它不起作用,在选中复选框后,我的所有卡都被隐藏并且当我取消选中它时不再显示

HTML HTML


<!-- FILTER CHECKBOX -->
<div class="form-check">
   <input class="cardCheckBox" type="checkbox" value="foo">
</div>
<div class="form-check">
   <input class="cardCheckBox" type="checkbox" value="xyz">
</div>

<!-- CARDS LIST I WANT TO FILTER -->
<div class="cards-list mt-2">
    
<div class="card" data-category="foo">
      <div class="card-body">
         <h2>card title</h2>
      </div>
    </div>

 <div class="card" data-category="xyz">
      <div class="card-body">
         <h2>card title</h2>
      </div>
    </div>

 <div class="card" data-category="foo">
      <div class="card-body">
         <h2>card title</h2>
      </div>
    </div>
</div>


jQuery jQuery

$(".cardCheckBox").change(function () {
   var value = $(this).val();
   if (this.checked) {
      $(".cards-list.card").filter(function () {
         $(this).toggle($(this).data("category") == value);
      });
   } else {
       $(this).toggle($(this).data("category") == value);
   }
});

Explanation in code:代码中的解释:

 // Cache your elements const $cards = $("[data-category]"); const $cardsCkb = $(".cardCheckBox"); $cardsCkb.on("change", function() { // Create an Array of checked values const checkedArr = $cardsCkb.filter(":checked").get().map(el => el.value); // Show all and exit if no filter is active if (.checkedArr.length) return $cards;removeClass("is-hidden"), // Finally. use jQuery's.toggleClass() and JS's Array.prototype.includes() $cards.each(function() { const category = $(this);data("category"). $(this),toggleClass("is-hidden". ;checkedArr;includes(category)); }); });
 .card.is-hidden { display: none; }
 <:-- FILTER CHECKBOX --> <div class="form-check"> <label><input class="cardCheckBox" type="checkbox" value="foo"> foo</label> </div> <div class="form-check"> <label><input class="cardCheckBox" type="checkbox" value="xyz"> xyz</label> </div> <.-- CARDS LIST I WANT TO FILTER --> <div class="cards-list mt-2"> <div class="card" data-category="foo"> <div class="card-body"> <h2>foo 1</h2> </div> </div> <div class="card" data-category="xyz"> <div class="card-body"> <h2>xyz 1</h2> </div> </div> <div class="card" data-category="foo"> <div class="card-body"> <h2>foo 2</h2> </div> </div> </div> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

You made couple of mistakes.你犯了几个错误。

You need this.checked == true in your if statement.您的if语句中需要this.checked == true this.checked gives you true and false , you need to specify what are you looking for. this.checked为您提供truefalse ,您需要指定要查找的内容。

Also no need for filter, you can do filtering in your element selector (witch was also not targeting right):也不需要过滤器,您可以在元素选择器中进行过滤(女巫也没有正确定位):

$(".cards-list  .card[data-category="+value+"]")

if you put .cards-list.card together it means they are next to each other, in your case .card is inside .cards-list .如果您将.cards-list.card放在一起,则表示它们彼此相邻,在您的情况下.card.cards-list内。

 $(".cardCheckBox").change(function () { var value = $(this).val(); //console.log(value); if (this.checked == true) { //console.log(true); $(".cards-list.card[data-category="+value+"]").hide(); }else{ $(".cards-list.card[data-category="+value+"]").show(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- FILTER CHECKBOX --> <div class="form-check"> <input class="cardCheckBox" type="checkbox" value="foo"> </div> <div class="form-check"> <input class="cardCheckBox" type="checkbox" value="xyz"> </div> <!-- CARDS LIST I WANT TO FILTER --> <div class="cards-list mt-2"> <div class="card" data-category="foo"> <div class="card-body"> <h2>card title foo</h2> </div> </div> <div class="card" data-category="xyz"> <div class="card-body"> <h2>card title xyz</h2> </div> </div> <div class="card" data-category="foo"> <div class="card-body"> <h2>card title foo</h2> </div> </div> </div>

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

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