简体   繁体   English

Minicart.js-项目已添加到购物车,但购物车未显示

[英]Minicart.js - Items are added to cart but cart not displaying

I have a click function on a button that adds an item to my cart using the Minicart.js library from minicartjs.com. 我在按钮上具有单击功能,可以使用minicartjs.com的Minicart.js库将商品添加到购物车中。 When the button is clicked items are being added to the cart however, the cart is not popping up as expected. 但是,单击按钮时,正在将商品添加到购物车中,但购物车并未如预期那样弹出。 I've tested this on up to date versions of Chrome and IE (ie 11). 我已经在最新版本的Chrome和IE(即11)上对此进行了测试。

Some things I've noticed: 我注意到的一些事情:

  • In the debugger if I execute the show cart function paypal.minicart.view.show() the cart displays fine. 在调试器中,如果我执行显示购物车功能paypal.minicart.view.show()则购物车会正常显示。 Even with the items I've added. 即使有我添加的项目。
  • When my add button is clicked "class" is appended to the body with no actual class assigned: 单击我的添加按钮时,“类”将附加到主体,而未分配任何实际的类:

chrome调试器显示带有class和ppminicart div的正文标签

  • When the cart is showing a class "minicart-showing" is appended to the body. 当车示出类“minicart-表示”被附加到主体。

The following script is at the end of a MVC partial View: 以下脚本位于MVC部分视图的末尾:

<script src="~/Scripts/minicart.js"></script>
<script>
    $(".showcart").click(function () {
        var data = $(this).attr("data-id");   
        paypal.minicart.cart.add(JSON.parse(data));
        // $("#body").toggleClass("minicart-showing"); <---doesn't work
        // paypal.minicart.view.show() <---- doesn't work
    });
paypal.minicart.render();
</script>

After further reviewing the minicartjs examples in the author's repository. 在进一步查看作者存储库中的minicartjs示例之后。 I found that using e.stopPropagation() within the click function resolves the issue. 我发现在click函数中使用e.stopPropagation()可以解决此问题。

   $(".showcart").click(function (e) { // <--- added the e function
    var data = $(this).attr("data-id");
    e.stopPropagation(); // <--- And this line.
    paypal.minicart.cart.add(JSON.parse(data));
});

Code Example From Author's GitHub Repo 作者的GitHub存储库中的代码示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM