首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JSON从服务器中删除项目

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

Stack Overflow用户
提问于 2015-03-21 13:57:23
回答 1查看 212关注 0票数 0

我真的很感激你能帮上忙。我的JSON知识有限,我解决不了这个问题。

我需要从我通过JSON创建的购物车快速视图中删除一个项目。前端似乎可以工作,但事实并非如此,因为它没有从JSON对象中删除该项。

JSON对象如下所示。

代码语言:javascript
复制
[
    {
        "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函数:

代码语言:javascript
复制
//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!');
            }
        });
    });
});

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

EN

回答 1

Stack Overflow用户

发布于 2015-03-21 14:11:35

我甚至不知道您要从数组中删除项的位置。我的jQuery有点生疏了,但我只看到DOM操作正在进行。

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

代码语言:javascript
复制
   // 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);
            }
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29179745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档