通过勾选复选框,让此函数扩展数组(组):
$("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)。
答案 0 :(得分:1)
您可以使用splice从数组中删除元素。
array = ['my', 'name', 'my'];
index = array.indexOf('my');
array.splice(index, 1);