简体   繁体   English

如何使用点击功能删除对象中的数据?

[英]How do I delete data in an object with my click function?

I'm putting together a list that I want to eventually turn into a martial arts "journal". 我整理了一份清单,希望最终成为武术“新闻”。 I currently have a click function for my delete key that will remove the DOM element. 我目前有一个用于删除键的click函数,它将删除DOM元素。 However, I'm unsure of how to delete the data that is stored in the object 但是,我不确定如何删除存储在对象中的数据

var curBelt = $('#belt').val();
//This is my data object that holds the array per belt
var data = {
    white: [],
    blue: [],
    purple: [],
    brown: [],
    black: [],
}
//This is my list
var $list = $('#todoList');

//This is a change function that take the value of the belt and runs the createListFromData function
//to reapply the svaed data in the array
$('#belt').change(function(){
    curBelt = $(this).val();
    createListFromData();
});
//This function adds the checkbox, itemtext, and the click function to cross off items
function addNewItem (itemKey, itemText) {
    var $listItem = $('<li>');
    var $span = $('<span>' + itemKey + ' - '+ itemText +'</span>');
    var $editButton = $('<button id = "editButton">Edit</button>')
    var $deleteButton = $('<button id = "deleteButton">Delete</button>')
    $listItem.append($span);
    $list.append($listItem);
    $list.append($editButton);
    $list.append($deleteButton);

    $deleteButton.click(function() {
        $listItem.remove();
        $editButton.remove();
        $deleteButton.remove();          
    });

}

var totalItems = 0;
var $inItemText  = $("#inItemText");
var $itemKey = $("#itemKey");
console.log($itemKey);
$itemKey.focus();
//This uses enter to push the text to my belt arrays and calls the    addsNewItem function to the list
$inItemText.on('keyup', function (event) {
    if (event.which === 13) {
        totalItems++;

        var inputObject = {};
        var itemText = $(this).val();
        var itemKey = $itemKey.val();

        if (!itemText || itemText === "") {
            return false;
        }
        inputObject.move = itemKey;
        inputObject.note = itemText;

        data[curBelt].push(inputObject);
        console.log(data);

        addNewItem(itemKey, itemText);

        $(this).val('');
        $itemKey.val('');
        inItemText.focus();
    }
});

//This empties the list and reapplies the arrays in the data object
var createListFromData = function () {  
    $list.empty();
    data[curBelt].forEach(function (object) {
        addNewItem (object.move, object.note);
    });
}
createListFromData();

Any help is appreciated! 任何帮助表示赞赏!

First thing is I would do is to move totalItems++; 我要做的第一件事是移动totalItems++; down to after 直到之后

if (!itemText || itemText === "") { return false; }

This will prevent you from adding to the total if its a blank element. 这将阻止您将总计(如果为空白元素)添加到总计中。 I'm also not sure why you would have your delete click event initialized in your addNewItem function. 我也不确定为什么要在addNewItem函数中初始化删除点击事件。 Do you mind making a jsfiddle or codepen to review? 您介意制作jsfiddle或Codepen进行审查吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM