我真的很感激你能帮上忙。我的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!');
}
});
});
});我认为我没有正确删除该项目。
发布于 2015-03-21 14:11:35
我甚至不知道您要从数组中删除项的位置。我的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);
}
}
}https://stackoverflow.com/questions/29179745
复制相似问题