在取消选中时从javascript数组中删除项目

时间:2014-07-11 08:32:36

标签: javascript jquery

通过勾选复选框,让此函数扩展数组(组):

$("input[type=checkbox]").click(function() {
     if($(this).is(":checked")){
         $(this).parent().data("groups").push("my");
          console.log($(this).parent().data("groups"));
      }
      else{
      //   $(this).parent().data("groups").pop();
      console.log($(this).parent().data("groups"));
       }

});

    <div class="item white" data-kpi="GDI" data-groups='["all", "numbers", "green", "square"]'>
<input type="checkbox" class="ps" name="add" value="my" />
</div>

我希望能够通过取消选中复选框再次删除该项目(可能有多个'my'group-items)。

1 个答案:

答案 0 :(得分:1)

您可以使用splice从数组中删除元素。

array = ['my', 'name', 'my'];
index = array.indexOf('my');
array.splice(index, 1);