【问题标题】:Jquery add to cartjQuery 加入购物车
【发布时间】:2015-05-28 17:47:31
【问题描述】:

我想创建一个马卡龙电子商务网站,用户在其中选择一个固定盒子的马卡龙(比如 1 个盒子意味着 10 个马卡龙)并将它们添加到购物车中。所以场景是,通过单击左侧面板中的产品(参见屏幕截图 SS1.png),相同的产品将出现在盒子上,依此类推。我已经通过 JQuery 完成了必要的工作并且工作正常。

当用户在框中填写所需的马卡龙并单击添加到购物车按钮时,产品将作为“套装/组”添加到购物车中。所以购物车看起来像第二个屏幕截图 - SS2.png。您能告诉我如何将产品添加到购物车中吗?该网站使用 PHP 和 MySQL。

截图如下:

等待您的回复。

【问题讨论】:

  • 你试过使用 Angular js 吗?我认为它会让你更容易实现这个应用程序

标签: jquery


【解决方案1】:

在我看来,您正在寻找 AJAX 功能

你可以这样写一些代码: 我假设所选项目显示在具有某种独特 Ids

的 div 中
$(document).ready(function() {
    $('#addtocartbutton').click(function() {
        // here you can loop through the div or something to get the selected items
           var quantities = {};  //creating an empty string to store the productids and quantities selected
       $('selecteditemsdiv').each(function(){
          var itemid = $(this).data('id');//get the item id assuming it is stored in the data-* attributes
          var qty = $(this).data('qty'); //get the quantity if you have stored it in data-* attributes
        just push it in the json string
           quantities['itemid'] = qty;

        });
        $.ajax({
            url: "cart.php",
            type: "POST",
            data: {datainfo: quantities}, //pass the json string to php
            success: function(data) {
                // Do stuff when the AJAX call returns
            }
        });
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多