简体   繁体   English

过滤项目选中复选框

[英]filter item with checked checkbox

I just want to make a filter with checkbox array using jquery and javascript. 我只想使用jquery和javascript制作一个带有checkbox数组的过滤器。 It works fine when only one checkbox is selected but when I select two or more checkboxes it doesn't show all the divs. 当只选中一个复选框时,它可以正常工作,但是当我选择两个或更多复选框时,它不会显示所有的div。 Could you please help me to find my mistake. 你能不能帮我找到我的错误。 I want to filter through the filter array. 我想通过过滤器数组进行过滤。 The name of our array is FlyList 我们的数组名称是FlyList

 let FlyList = [ { id: "1", Airline_number: "961", Type_ticket: "systemi", Airline: "dubai", fly_time: "04:00-08:00", Class_type: "Economical" }, { id: "2", Airline_number: "962", Type_ticket: "charteri", Airline: "frans", fly_time: "08:00-11:00", Class_type: "Commercial" }, { id: "3", Airline_number: "963", Type_ticket: "systemi", Airline: "Emirates", fly_time: "11:00-14:00", Class_type: "Commercial" }, { id: "4", Airline_number: "964", Type_ticket: "systemi", Airline: "Emirates", fly_time: "14:00-17:00", Class_type: "Economical" }, { id: "5", Airline_number: "965", Type_ticket: "charteri", Airline: "dubai", fly_time: "17:00-21:00", Class_type: "Commercial" }, { id: "6", Airline_number: "966", Type_ticket: "charteri", Airline: "frans", fly_time: "21:00-24:00", Class_type: "Economical" } ]; function customFilter(list, field, value) { let fill = list.filter(item => { if (typeof filter === "object") { value.forEach(val => { return item[field] === value; }); } }); console.log(fill); } let filterCheckbox = document.querySelector(".customCheck"); filterCheckbox.addEventListener("change", e => { e.preventDefault(); // jquery // var field = $(this).parents('.filter_type').attr('data-field'); let field = document.querySelector(".filter_type").getAttribute("data-field"); // jquery let val = e.target.value; customFilter(FlyList, field, val); // console.log(val); }); <!-- begin snippet: js hide: false console: true babel: false --> 

 <div class="filters"> <div class="collapse show" id="collapseFilters"> <div class="filter_type" data-field="Type_ticket"> <h6> Type of ticket <div class="switcher float-left "> <label class="customToggle small"> <input type="checkbox" name="" id="" class="selectall" /> all Type of ticket <div class="indicator"></div> </label> </div> </h6> <ul> <li> <label class="customCheck w-100 " id="filter-check"> <input type="checkbox" name="" value="systemi" class="individual" /> systemi <div class="indicator"></div> </label> </li> <li> <label class="customCheck w-100" id="filter-check2"> <input type="checkbox" name="" value="charteri" class="individual" /> charteri <div class="indicator"></div> </label> </li> </ul> </div> <div class="filter_type airlineSelection" data-field="Airline"> <h6> airline <div class="switcher float-left"> <label class="customToggle small"> <input type="checkbox" name="" id="" /> all airline <div class="indicator"></div> </label> </div> </h6> <ul> <li> <label class="customCheck" id=""> <input type="checkbox" value="frans" name="inlineRadioOptions" /> frans <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" name="inlineRadioOptions" /> dubai <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="emirates" name="inlineRadioOptions" /> emirates <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="frans" name="inlineRadioOptions" /> frans <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="emirates" name="inlineRadioOptions" /> emirates <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="dubai" name="inlineRadioOptions" /> dubai <div class="indicator"></div> </label> </li> </ul> </div> <div class="filter_type" data-field="Class_type"> <h6> Class_type <div class="switcher float-left"> <label class="customToggle small"> <input type="checkbox" name="" id="" /> all Class_type <div class="indicator"></div> </label> </div> </h6> <ul> <li> <label class="customCheck" id=""> <input type="checkbox" value="economical" name="inlineRadioOptions" /> economical <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="commercial" name="inlineRadioOptions" /> commercial <div class="indicator"></div> </label> </li> </ul> </div> <div class="filter_type" data-field="fly_time"> <h6> fly_time <div class="switcher float-left"> <label class="customToggle small"> <input type="checkbox" name="" id="" /> all fly_time <div class="indicator"></div> </label> </div> </h6> <ul> <li> <label class="customCheck" id=""> <input type="checkbox" value="04:00-08:00" name="inlineRadioOptions" /> 04:00-08:00 <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="08:00-11:00" name="inlineRadioOptions" /> 08:00-11:00 <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="11:00-14:00" name="inlineRadioOptions" /> 11:00-14:00 <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="14:00-17:00" name="inlineRadioOptions" /> 14:00-17:00 <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="17:00-21:00" name="inlineRadioOptions" /> 17:00-21:00 <div class="indicator"></div> </label> </li> <li> <label class="customCheck"> <input type="checkbox" value="21:00-24:00" name="inlineRadioOptions" /> 21:00-24:00 <div class="indicator"></div> </label> </li> </ul> </div> </div> </div> 

With querySelector you select only one element. 使用querySelector您只能选择一个元素。 That's why you add an event listener to only first .customCheck checkbox. 这就是为什么你只在第一个.customCheck复选框中添加一个事件监听器。

You need to have an event listener attached to all the checkboxes. 您需要将一个事件侦听器附加到所有复选框。 To do so you can use querySelectorAll instead of querySelector and then attach an event to each input: 为此,您可以使用querySelectorAll而不是querySelector ,然后将事件附加到每个输入:

let filterCheckboxes = document.querySelectorAll('.customCheck');

filterCheckboxes.forEach(checkbox => checkbox.addEventListener('change' , (e) => { 
  console.log(e);
  // filtering goes here
}))

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

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