<body class="container-fluid">
<div class="container">
<h1 id = "changeBg" title = "Click here to change the background"> My To-do List </h1>
<form>
<div class = "inputList">
<input type="text" id="inputUser" placeholder="Type your task here - minimum four characters"
class="textfield">
<ul id="list"> </ul>
</div>
<div class = "myBtn">
<button type= "button" id ="addBtn" title = "Add Task"><i class="fas fa-plus"></i></button>
<button type= "button" id ="removeBtn"><i class="fas fa-trash-alt"></i></button>
</form>
</div>
这是我的 html 文件
$(function () {
var myTaskArray = [];
$("#addBtn").click(function () {
var newItem = $('#inputUser').val();
myTaskArray.push(newItem);
if (myTaskArray.length > 10) {
alert("Your Schedule for today is now full!");
$('#inputUser').val('');
return;
} else if (newItem.trim() == "" || newItem.length < 3) {
alert("You need to write a task");
} else {
$("#list").append("<li><input type = 'checkbox' id = 'checkbox'>" + " " + newItem + "</li>");
$("input:checkbox").click(function () {
var $this = $(this);
if (this.checked) {
$this.parent().addClass('completed');
} else {
$this.parent().removeClass('completed');
}
});
}
$('#inputUser').val('');
});
//remove item on the list
$("#removeBtn").click(function () {
$('#list').children().filter(function () {
return this.firstChild.checked;
}).remove();
});
});
这是我的 js 文件。我设置了最多 10 个用户输入的限制,它按原样工作,但是当我通过删除按钮删除一些值时,该值的索引不会从数组中删除并使其再次可用于输入。无论如何我可以解决这个问题吗?
慕容708150
相关分类