[英]Save class in array + splice deleting wrong object
Fiddle with html and javascript: https://jsfiddle.net/4a4u1jg1/ 用html和javascript拨弄: https : //jsfiddle.net/4a4u1jg1/
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.