我已经阅读了我的待办事项列表应用程序的拼接方法,但我不知道为什么这不起作用。 https://codepen.io/dev999/pen/XRzMwx有人可以告诉我如何修复我的拼接方法,以便删除执行项目的权利吗?谢谢!到目前为止,它似乎只删除了第一个项目,无论什么
function dele (position, amount) {
var amount = 1;
var position =
((parseInt(prompt("What is the number of the item you want to delete?")))+1);
var position = todo[position];
todo.splice(position, amount);
}
答案 0 :(得分:0)
我不想改变太多东西来混淆你。
第一次我在您的HTML中添加了My todo list
以显示当前的待办事项列表。
dispTodo();
或add
更新当前待办事项列表时,我都会致电dele
。
第3个数组索引从0,1,2,3开始作为位置1,2,3,4所以您需要输入-1而不是+1(例如:第1项的索引为0)
第4个todo.splice(position, 1);
只会从数组中删除该项目。
var todo = [];
// creating empty todo array
// todo function to add a to do one by one
function addTodo() {
todo.push(prompt("Enter one item you want to add to your todo!"));
dispTodo();
}
function dispTodo() {
document.getElementById("todolist").innerHTML = todo;
}
function dele(position, amount) {
var position =
parseInt(prompt("What is the number of the item you want to delete?")) - 1;
todo.splice(position, 1);
dispTodo();
}

<button onclick="addTodo()"> Click here to add one to do of your choice to your list</button>
<button onclick="dele()"> Click here to delete a to do of your choice</button>
<button onclick="dispTodo()"> Click here to display your list of todos</button>
<div>My todo list:
<div id="todolist"></div>
</div>
&#13;
答案 1 :(得分:0)
假设todo
在全局范围内,此函数不需要任何参数。
function delete() {
var choice = prompt("What is the number of the item you want to delete?")
// need to call parseInt with 10 to ensure base10
var pos = parseInt(choice, 10) + 1
// remove 1 item, starting at `pos`
todo.splice(pos, 1)
}
这里有一些推荐阅读
答案 2 :(得分:0)
您已经不必要地覆盖了位置变量。 而且如果你想删除正确的列表项,你必须改变这一行
parseInt(prompt("What is the number of the item you want to delete?"))+1
到
parseInt(prompt("What is the number of the item you want to delete?"))-1