javascript - 在数组中保存类+拼接删除错误的对象

标签 javascript android jquery ios arrays

摆弄 html 和 javascript: https://jsfiddle.net/4a4u1jg1/

  1. 我希望使用“.finished”类保存在数组中的元素与数组中的类一起保存,如果我再次点击该元素并删除该类,则它们应该在不包含类的情况下保存数组,我该如何实现这个?

2a。当我双击一个项目时,它会删除屏幕上的正确项目,但在我重新加载应用程序后,它会删除列表中的最后一个项目,换句话说,它会从 html 中删除正确的项目,但会删除数组中的最后一个项目。 解决了!

2b。此外,我无法删除新添加的项目(或标记为已完成),我必须重新打开/更新应用程序/浏览器。

$("li").dblclick(function()
        {
            //Removes last task instead of the task I double tapped on
            //and I can't remove newly added tasks
            taskListArray.splice($.inArray($(this), taskListArray, 1));

            $(this).remove();

            if(window.localStorage)
            {
                window.localStorage.setItem("taskList", JSON.stringify(taskListArray));
            }
        });

如果有人能帮助我解决问题 #1 和 #2b,那就太好了,谢谢!

最佳答案

尝试使用 <li>拼接的索引,因为实际元素不是数组中的元素:

改变:

taskListArray.splice($.inArray($(this), taskListArray, 1));

taskListArray.splice($(this).index(),1);

另请注意 splice() 的第三个参数用于添加到数组

关于javascript - 在数组中保存类+拼接删除错误的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43352936/

相关文章:

android - 如何获取选定的索引 JQuery Mobile Listview

javascript - jquery 捕获上一节课的问题

javascript - 如何通过 JSON 传递函数名称并在 javascript/jQuery 中调用它?

javascript - 在javascript中动态调用本地函数

javascript - 使用 Node js 和 Express 创建 Web 代理

javascript - Angular 4 http 未返回响应

android - LinearLayout 在 ScrollView Android 中被切断

android - 使用 Easytracker 的谷歌分析中的未捕获异常

javascript - 在 jQuery 中, `this.element` 和 `this.element[/* some number */]` 有什么区别?

javascript - 使用branch.io在javascript中重定向到应用程序