使用JSON从服务器中删除项目

时间:2015-03-21 05:57:23

标签: jquery json

我真的很感激这里的一些帮助。我的JSON知识有限,我无法解决。

我需要从通过JSON创建的购物车快速视图中删除项目。前端似乎有效,但它没有,因为它不会从JSON对象中删除该项。

JSON对象看起来像。

[
    {
        "id": 216687,
        "productId": 9604505,
        "catalogId": 306758,
        "name": "xxxxxxxxx1",
        "description": "",
        "quantity": 1,
        "totalPrice": "38,00",
        "smallImage": "/images/products/large/xxxxx.jpg",
    },
    {
        "id": 216687,
        "productId": 9604503,
        "catalogId": 306756,
        "name": "xxxxxxxxx1",
        "description": "",
        "quantity": 1,
        "totalPrice": "38,00",
        "smallImage": "/images/products/large/xxxxx.jpg",
    }
]

jQuery函数:

//Deleting Items
$(document).on('click', '.shopping-cart .delete i', function(){
    var $target = $(this).parent().parent();
    var $positions = $('.shopping-cart .item');
    $target.hide(300, function(){
        $.when($target.remove()).then( function(){
            if($positions.length === 1) {
                $('.shopping-cart .items-list').remove();
                $('.shopping-cart .title').text('Shopping cart is empty!');
            }
        });
    });
});

我认为我没有正确删除该项目。

1 个答案:

答案 0 :(得分:0)

我看不到你甚至试图从数组中删除该项目。我的jQuery有点生疏,但我只看到DOM操作正在进行中。

您必须在列表中找到该项并将其删除。

   // code elsewhere
    var id = 216687;

    RemoveItem(id);

    // function to remove item
    function RemoveItem(id) {    
        for(var i = 0; i < data.length; i++) {
            // don't know what your criteria for removal is, but you can match any property
            if (data[i].id == id) {
                // removes the element when the match is found based on your criteria for removal
                var smallerArray = data.splice(i, 1);
            }
        }
    }
相关问题