0

I have an array which I churn out using $.each (not jquery but appframework, which is alike). But I need to delete an item, and splice and delete don't seem to work.

var cart=Array();
cart[0]=['Beef', 20, 1, 'Well done'];
cart[1]=['Chicken', 15, 1, 'no samonella please'];

$.each(cart,function(index,val){
  $('#cart').append('<div class="devider" data-index="'+index+'">'+val[0]+'<span onclick="delete_item(this);">&#215;</span></div>');
});

function delete_item(that) {
    delete cart[$(that).closest('.devider').attr('data-index')]; // doesn't work
    cart.splice($(that).closest('.devider').attr('data-index'),1); // doesn't work
    cart.splice(1,1); // doesn't work
}

So I tried using parseInt(), cause you know, why not. But obviously that didn't work. Am I doing something wrong or do I have to use an alternative way.

Like I said, I'm using appframework (jqmobi). So best would be to not rely on jquery seeing appmobi doesn't have all the functions that jquery does. But all js should work.

To be thorough, if I alert $(that).closest('.devider').attr('data-index') it gives back the "each" index correct.

4

0 回答 0