我试图通过单击从列表中删除项目,我缺少什么?

时间:2019-01-06 20:44:54

标签: javascript jquery

我试图在每次单击项目时都将其删除,但是在尝试创建“待办事项”列表时,一次只能删除一个项目(被单击的项目)。我可以轻松地同时删除所有内容,但是尝试在单个级别上执行操作时遇到很多问题。我以为这可以用,但是希望能引起更多关注。

 var toDoCount = 0;
 var todoarray = [];

window.onload = function() {
//user clicked on the add button in the to-do field add that text into the to-do text
$('#add-to-do').on('click', function(event) {
    event.preventDefault();
    //assign variable to the value entered into the textbox
    var value = document.getElementById('to-do').value; 
    //test value
    console.log(value);

    var todoitem = $("#to-dos");
    todoitem.attr("item-");
    //prepend values into the html and add checkmark, checkbox, and line break to make list
    var linebreak = "<br/>";
    var todoclose = $("<button>");
    todoclose.attr("data-to-do", toDoCount);
    todoclose.addClass("checkbox");
    todoclose.text("☑");

    //prepend values to html
    $("#to-dos").prepend(linebreak);
    $("#to-dos").prepend(value);
    $("#to-dos").prepend(todoclose);
    toDoCount++;

todoarray.push(value);

console.log(todoarray);

//to remove item from checklist
$(document.body).on("click", ".checkbox", function() {
var toDoNumber = $(this).attr("data-to-do");
$("#item-" + toDoNumber).remove();

});


});

}

HTML在下面

                <div class ="col-4">
                <!-- To Do List -->
                <form onsubmit= "return false;">
                    <span id = "todo-item" type = "text">
                    <h4>Add your Agenda Here</h4>
                    <input id ="to-do" type = "text">
                    <input id ="add-to-do" value = "Add Item" type = "submit">
                    </span>
                </form>
             <div id="to-dos"></div>
            </div>

1 个答案:

答案 0 :(得分:1)

不需要数字,只需要元素。

更改...

public class TimerLabel : Label
{
    private bool beating;
    public TimerLabel()
    {
        StartHeartbeat();
    }

    public void StartHeartbeat()
    {
        //only start beating again if not currently beating.
        if (!beating)
        {
            beating = true;
            Heartbeat();
        }
    }

    public void StopHeartbeat()
    {
        beating = false;
    }

    async void Heartbeat()
    {
        while (beating)
        {
            this.Text = DateTime.UtcNow.ToLongTimeString();
        }
    }
}

到...

$("#item-" + toDoNumber).remove();

例如

$(this).remove();