[英]JQuery splice array deleting wrong
I'm making a hybrid app with cordova and I have two problems 我正在用cordova制作混合应用程序,但有两个问题
When I double tap an item in my list it removes the correct item, however when I close and re-open the app it has removed the last one in the list instead, so basically, it removes the one I double tap on screen but in the array it always removes the last item in the list. 当我双击列表中的一个项目时,它会删除正确的项目,但是当我关闭并重新打开该应用程序时,它将删除列表中的最后一个项目,因此,基本上,它会删除我在屏幕上双击的那个项目,但是在数组,它总是删除列表中的最后一项。
I can't remove a item i just added from the list, I need to close and re-open the app to remove it. 我无法从列表中删除刚刚添加的项目,我需要关闭并重新打开应用程序才能将其删除。
The problems occur in the bottom function 问题出现在底部功能
Here's my javascript file(JQuery): 这是我的JavaScript文件(JQuery):
var taskListArray = new Array();
$(document).ready(function()
{
var addInput = $("#addInput");
var taskList = $("#taskList");
if(window.localStorage)
{
taskListArray = JSON.parse(window.localStorage.getItem("taskList"))
}
else
{
window.plugins.toast.showLongCenter("LocalStorage not found, saving unavailable!");
}
if(taskListArray != null)
{
for(i = 0; i < taskListArray.length; i++)
{
var task = "<li>" + taskListArray[i].task + "</li>";
taskList.append(task);
}
}
else
{
taskListArray = new Array();
}
$("#addButton").on("click", function()
{
if($("#addInput").val() != 0)
{
var task = "<li>" + addInput.val() + "</li>";
taskList.append(task);
taskListArray.push({task:addInput.val()});
if(window.localStorage)
{
window.localStorage.setItem("taskList", JSON.stringify(taskListArray));
}
addInput.val("");
window.plugins.toast.showShortCenter("Task added!");
}
});
$("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();
window.plugins.toast.showShortCenter("Task removed!");
if(window.localStorage)
{
window.localStorage.setItem("taskList", JSON.stringify(taskListArray));
}
});
});
Edit: Still no answer to question #2? 编辑:对问题2仍然没有答案? I would really appreciate answers to both questions or one at least, thanks!
非常感谢您对两个问题的回答,或者至少一个,谢谢!
The $.inArray
needs two arguments, but you are only passing one. $.inArray
需要两个参数,但是您只传递了一个。 With only one parameter the function returns -1 and the splice
function removes the last element when the argument is -1. 仅带有一个参数的函数将返回-1,并且在参数为-1时,
splice
函数将删除最后一个元素。 It should be something like this taskListArray.splice($.inArray($(this), taskListArray), 1);
它应该像这个
taskListArray.splice($.inArray($(this), taskListArray), 1);
. 。 JQuery inArray Doc
jQuery inArray文档
Can you please replace your code taskListArray.splice($.inArray($(this)), 1); 您能否替换代码taskListArray.splice($。inArray($(this)),1); with taskListArray.splice($.inArray($(this),taskListArray), 1);
与taskListArray.splice($。inArray($(this),taskListArray),1); and try.
并尝试。
In the jQuery documentation its stated that you need to provide the array in which u need to search. 在jQuery文档中,它指出您需要提供您需要在其中搜索的数组。
jQuery.inArray( value, array [, fromIndex ] ) jQuery.inArray(value,array [,fromIndex])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.