简体   繁体   English

从动态数组中删除项目

[英]Remove items from a dynamic array

I have some lists and checkboxes which work as filters, what I need to do is when you select a checkbox is to add that value to a small tag next to the title and when uncheck then remove that value. 我有一些用作过滤器的列表和复选框,我需要做的是,当您选中一个复选框时,就是将该值添加到标题旁边的小标签中,而当取消选中然后删除该值时。

I am making this work, saving each value into an array and when uncheck try to look for the index of that value and remove, but I think I have a conflict with the positions in the array. 我正在做这项工作,将每个值保存到数组中,当取消选中时,尝试查找该值的索引并删除,但是我认为我与数组中的位置存在冲突。 If you click one by one and then remove one by one then it works but if you click all of them and uncheck only the first one, it removes everything. 如果单击一个,然后一个一个地删除,则它起作用,但是,如果您单击所有它们,然后仅取消选中第一个,则它将删除所有内容。 Have a clue how to solve it but not sure how to proceed. 了解如何解决问题,但不确定如何进行。

Any help please? 有什么帮助吗? https://jsfiddle.net/panconjugo/qsgwvp63/2/ https://jsfiddle.net/panconjugo/qsgwvp63/2/

$(function() {
  $(".filter-nav > li").each(function(index) {
    var brands = [];
    $(this).find("input:checkbox").each(function(index) {
      $(this).on("click", function() {
        if ($(this).is(":checked")) {
          console.log("adding: " + index);
          brands.push($(this).val());
          console.log(brands);
          $(this).parent().parent().prev().find(".selected-group").text(brands.join(", "));
        } else {
          console.log("removing:" + index);
          brands.splice(index);
          console.log(brands);
          $(this).parent().parent().prev().find(".selected-group").text(brands.join(", "));
        }
      });
    });
  });
});

change the code brands.splice(index); 更改代码brands.splice(index); to brands.splice(brands.indexOf($(this).val()), 1); brands.splice(brands.indexOf($(this).val()), 1);

first you have to find the item index from the array then use that index to remove from the array 首先,您必须从数组中找到项目索引,然后使用该索引从数组中删除

Instead of maintaining an array by adding/removing items, it is much, much simpler to hook to the change event of the checkboxes and then build a new array using map() which contains the data of the selected items in that group. 与其通过添加/删除项目来维护数组,不如钩住复选框的change事件,然后使用map()构建一个包含该组中所选项目数据的新数组,要简单得多。 You can then join this together and display the text as needed. 然后,您可以将其连接在一起并根据需要显示文本。 Try this: 尝试这个:

 $('.filter-nav input:checkbox').on("change", function() { var $parent = $(this).closest('.filter-nav > li'); var items = $parent.find('input:checkbox:checked').map(function() { return this.value; }).get(); $parent.find('.selected-group').text(items.join(', ')); }); 
 .filter-nav { list-style: none; padding: 0; } .filter-nav li { line-height: 40px; } .filter-nav > li > h3 { background-color: #222; color: white; padding-left: 20px; cursor: pointer; } .selected-group { color: yellow; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <ul class="filter-nav"> <li> <h3>Brand <small class="selected-group"></small></h3> <ul> <li> <label for="brand1">Brand1</label> <input type="checkbox" id="brand1" value="Brand 1"> </li> <li> <label for="brand1">Brand2</label> <input type="checkbox" id="brand2" value="Brand 2"> </li> <li> <label for="brand1">Brand3</label> <input type="checkbox" id="brand3" value="Brand 3"> </li> </ul> </li> <li> <h3>Size <small class="selected-group"></small></h3> <ul> <li> <label for="xs">XS</label> <input type="checkbox" id="xs" value="xs"> </li> <li> <label for="m">M</label> <input type="checkbox" id="m" value="m"> </li> <li> <label for="l">L</label> <input type="checkbox" id="l" value="l"> </li> </ul> </li> <li> <h3>Color <small class="selected-group"></small></h3> <ul> <li> <label for="blue">Blue</label> <input type="checkbox" id="blue" value="blue"> </li> <li> <label for="green">Green</label> <input type="checkbox" id="green" value="green"> </li> <li> <label for="red">Red</label> <input type="checkbox" id="red" value="red"> </li> </ul> </li> </ul> 

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

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