0

我正在尝试使用 JSONArrays 和 Objects 在本地存储数据。该程序所做的是获取我动态插入的几个滑块的值(有时是 1 个,有时是 2 个滑块,有时是 3 或 4 个......)。因此,我将产品的值和 id 与数组一起存储在本地。但是有一个问题它只保存了第一个数组,push方法不起作用。

这是代码:

function afegir_i_editar_llistacompra(){
    //Obtenim els valors dels sliders i els guardem a localhost 
    var id_product = $('body').data('product');
    var array_of_bought_colors_new = new Array({'id': id_product});
    var array_of_bought_colors_existing = get_product_info(id_product, 'cesta_list'); 

    for (var i = 0; $('#slider-'+i).val() != null ; i++) {
        array_of_bought_colors_new.push({'color':$('#slider-'+i).attr('name'),'value': $('#slider-'+i).val()});
    };

    if(array_of_bought_colors_existing != null){
        for (var i = array_of_bought_colors_existing.length - 1; i >= 1; i--) {
            if(array_of_bought_colors_existing[i].value != array_of_bought_colors_new[i].value)
                array_of_bought_colors_existing[i].value = array_of_bought_colors_new[i].value;
        };
    }else if( array_of_bought_colors_new.length > 1 && $('body').data('cesta_list') != null) {      
        $('body').data('cesta_list', $('body').data('cesta_list').push(array_of_bought_colors_new));
    }else{
        $('body').data('cesta_list', array_of_bought_colors_new);
    }
}

我要保存的内容:

[{id:1},/*First slider -->*/{'color':'blue','value':2},/*Second slider -->*/{'color':'blue','value':32}]

希望你能理解!

4

0 回答 0