2015-03-03 79 views
-1

當我點擊插件,它會打開一個彈出式按鈕,我可以選擇/取消選擇複選框,我正在更新這些數據到數據 - 這是一個數據 - 屬性。我怎樣才能從JSON刪除JSON數組

數據填充數據陣列中的數據看起來這樣

[ 
    { 
     "name": "checkbox-mini-0", 
     "cost": "20" 
    }, 
    [ 
     { 
      "name": "checkbox-mini-1", 
      "cost": "10" 
     } 
    ], 
    [ 
     { 
      "name": "checkbox-mini-4", 
      "cost": "199" 
     } 
    ], 
    [ 
     { 
      "name": "checkbox-mini-5", 
      "cost": "233" 
     } 
    ] 
] 

我的問題是,當我取消,我怎麼能刪除基於名稱的陣列?

http://jsfiddle.net/kgm9o693/15/

回答

2

我想你需要的是

// checkbox checked 
//var toppcrusts = []; 
$(document).on('click', '.ui-checkbox-off', function (event) { 
    var vendoritemsdata = $('.lastItm_Wrap').data('stuff'); 

    var checkboxid = $(this).next().attr("id"); 
    var cost = $(this).attr("cost"); 

    var toppcrusts = { 
     'name': checkboxid, 
     'cost': cost 
    }; 
    vendoritemsdata.push(toppcrusts); 
    $('.lastItm_Wrap').data('stuff', vendoritemsdata).attr('data-stuff', JSON.stringify(vendoritemsdata)); 
}); 

// checkbox Unchecked 
$(document).on('click', '.ui-checkbox-on', function (event) { 
    var vendoritemsdata = $('.lastItm_Wrap').data('stuff'); 

    var name = $(this).next().attr("id");; 
    $.each(vendoritemsdata, function (i, item) { 
     if(item.name == name){ 
      vendoritemsdata.splice(i, 1); 
      return false; 
     } 
    }); 
    $('.lastItm_Wrap').data('stuff', vendoritemsdata).attr('data-stuff', JSON.stringify(vendoritemsdata)); 
}); 

演示:Fiddle

注:.attr('data-stuff', JSON.stringify(vendoritemsdata))是不是真的需要?它只是因爲你的樣品中你是做更新屬性,而不是使用屬性使用數據api

此外還有一個輕微的變化數據結構,因爲我認爲陣列的數組並不是真的需要。

+0

非常感謝,但是我只想從數據數組中刪除未經檢查的特定數組。 (目前它正在刪除數據數組中的最後一個) – Kiran 2015-03-03 06:26:00

+1

@Kiran http://jsfiddle.net/arunpjohny/Lf9q56hw/2/ – 2015-03-03 06:30:07

+0

是的,這是我在找,非常感謝。 – Kiran 2015-03-03 06:31:46

相關問題