JSON将选择的项目保存在数组中并传递到其他页面


JSON save selected items on array and pass to other page

所以我有2页。在一个我有一个表,从JSON页面获取数据,我在每一行插入一个按钮,每当点击它应该得到数组上的项目。然后,该数组应该给出第二页的结果。

我可以毫无问题地从JSON中获取信息,但不能将信息存储在数组上。有人能帮帮我吗?我是JavaScript新手。

第1页:

 <script>
<!-- Retrieve data from file -->
var products=[];
var cartArray=[];
var product_array=[];
$.getJSON('products.json',function(data){
    $.each(data.products, function(i, f){
        var img = f.image_url;
        var title = f.title;
        var price = f.price;
        var old_price = f.old_price;
        product_array.push([img, title, price]);
        var tblCell = "<tr><td class='prod_container'><tr><td class='prod_img_container'><img class='prod_img' src=" + img + "></td></tr>" + "<tr class='title_container'><td class='title'>" + title + "</td></tr>" + "<tr class='price_container'><td class='price'>" + price + "</td>" + "<td class='price_org'>" + old_price + "</td>" + "<td class='add_cart'><img class='add_cart_img' src='img/buynow-green-6.png' onClick='addToCart()'>" + "</td></tr>"
        $(tblCell).appendTo("#list_products tbody");
    });
}); 
<!-- Add To Cart -->
function addToCart(){
    cartArray.push([product_array.title, product_array.img, product_array.price]);
    window.alert(cartArray);
}
<!-- Store and go to checkout -->
function goToCheckout(){
    $('#store').on('click', function(){
        localStorage.cartArray = cartArray;

    });
    window.location.href="cart.html";
}

不确定您最终需要的数据结构,但如果您想要一个多维数组:

var cartArray = []
function addToCart(product_array){
    cartArray.push([product_array.title, product_array.img, product_array.price];
    window.alert(cartArray);
}