简体   繁体   English

将 Jquery 代码通过复选框过滤列表转换为 Vanilla JS

[英]Convert Jquery code to filter list by checkbox into Vanilla JS

i have a code(found here) that filter a list by data-attribute element, and i need convert from jquery to js我有一个代码(在这里找到)通过数据属性元素过滤列表,我需要从 jquery 转换为 js

 $('.checkbox').change(function() { $('li.list').each(function(i, item) { var color = $(this).data('color'); var visible = $('input.checkbox[data-color="' + color + '"]:checked').length > 0; visible ? $(this).show() : $(this).hide(); }); if ($('input.checkbox:checked').length === 0) { $('li.list').show(); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" class="checkbox" data-color="blue"> Blue <input type="checkbox" class="checkbox" data-color="green"> Green <input type="checkbox" class="checkbox" data-color="red"> Red <ul> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> <li class="list" data-color="red">Red</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> </ul>

I tried to use a forEach loop but i don't know how to get the same result我尝试使用 forEach 循环,但我不知道如何获得相同的结果

const checkbox = document.querySelectorAll(".checkbox");
const list = document.querySelectorAll(".list");


 checkbox.forEach((item)=> {
  item.addEventListener('change', () => {

    var color = item.getAttribute('data-color');

    //Do another loop on list 




  })

 })

how to do that?怎么做?

One option is to construct a standalone function that checks the :checked checkboxes for the colors to show, then iterates through the .list s and sets the display style appropriately.一种选择是构建一个独立的函数,该函数检查:checked复选框以显示颜色,然后遍历.list并适当地设置display样式。 For every checkbox, add a change listener pointing to that function:对于每个复选框,添加一个指向该函数的更改侦听器:

 const checkbox = document.querySelectorAll(".checkbox"); const list = document.querySelectorAll(".list"); const examineList = () => { const checkedColors = [...document.querySelectorAll('.checkbox:checked')] .filter(input => input.checked) .map(input => input.dataset.color); const showColor = checkedColors.length ? color => checkedColors.includes(color) : color => true; // if no colors are selected, always show every <li> document.querySelectorAll('.list').forEach((li) => { li.style.display = showColor(li.dataset.color) ? 'list-item' : 'none'; }); }; checkbox.forEach((item) => { item.addEventListener('change', examineList); });
 <label><input type="checkbox" class="checkbox" data-color="blue"> Blue</label> <label><input type="checkbox" class="checkbox" data-color="green"> Green</label> <label><input type="checkbox" class="checkbox" data-color="red"> Red</label> <ul> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> <li class="list" data-color="red">Red</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> </ul>

I'd like to present a little more concise version of the accepted answer.我想提出一个更简洁的已接受答案版本。

I've added labels and a wrapping element around the checkboxes that serves as an anchor point to apply delegate listening to (so only listener needed in this code).我在复选框周围添加了标签和一个环绕元素,用作应用委托侦听的锚点(因此此代码中只需要侦听器)。

 colorfilter.addEventListener('change', (e) => { let selectedColors = [...colorfilter.querySelectorAll(':checked')].map(x => x.dataset.color); for (const listItem of document.querySelectorAll('.list')) { listItem.classList[selectedColors.includes(listItem.dataset.color) ? 'add' : 'remove']('show'); } })
 .list { display: none; } .show { display: list-item; }
 <div id="colorfilter"> <label><input type="checkbox" class="checkbox" data-color="blue"> Blue</label> <label><input type="checkbox" class="checkbox" data-color="green"> Green</label> <label><input type="checkbox" class="checkbox" data-color="red"> Red</label> </div> <ul> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> <li class="list" data-color="red">Red</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="blue">Blue</li> <li class="list" data-color="green">Green</li> </ul>

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

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