简体   繁体   English

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

[英]Save class in array + splice deleting wrong object

Fiddle with html and javascript: https://jsfiddle.net/4a4u1jg1/ 用html和javascript拨弄: https : //jsfiddle.net/4a4u1jg1/

  1. I want the elements saved in the array with the ".finished" class to be saved with the class in the array, and if I tap the element again and the class is removed, they should be saved without a class in the array, how do I accomplish this? 我希望使用“ .finished”类保存在数组中的元素与数组中的类一起保存,如果再次点击元素并删除该类,则应该在数组中没有类的情况下保存它们我要完成这个吗?

2a. 2a。 When I double tap an item it removes the correct item on screen but after I reload the app it has removed the last item in the list instead, in other words it deletes correct from html but last item in the array. 当我双击一个项目时,它将删除屏幕上正确的项目,但是在我重新加载应用程序后,它已删除了列表中的最后一个项目,换句话说,它从html中删除了正确的项目,但删除了数组中的最后一个项目。 SOLVED! 解决了!

2b. 2b。 Also I can't delete a newly added item (or mark as finished) I have to re-open/update the app/browser. 另外,我无法删除新添加的项目(或标记为完成),我必须重新打开/更新应用程序/浏览器。

$("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));
            }
        });

Would be awesome if someone could help me out with question #1 and #2b, thanks! 如果有人可以帮助我解决问题#1和#2b,那就太好了,谢谢!

Try using the <li> index for your splice since the actual element is not what is in the array: 尝试对接头使用<li>索引,因为实际元素不是数组中的元素:

Change: 更改:

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

To

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

Also note the third argument of splice() is for adding to array 另请注意,splice()的第三个参数用于添加到数组

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

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