简体   繁体   English

如何根据购物车数量使用ajax添加和删除类

[英]How to add and remove class using ajax based on cart count

My cart is incremented using an ajax call.我的购物车是使用 ajax 调用增加的。 I want to add a class when the counter is 1 and remove a class when the counter is 0. Can someone tell me how can I achieve this without a page load?我想在计数器为 1 时添加一个类并在计数器为 0 时删除一个类。有人能告诉我如何在没有页面加载的情况下实现这一点吗? Is there any ajax query which fires automatically when the counter increments and decrements?是否有任何 ajax 查询在计数器递增和递减时自动触发? https://prnt.sc/1xaim0a https://prnt.sc/1xaim0a
https://prnt.sc/1xaipj8 https://prnt.sc/1xaipj8

Why don't check after ajax call the value of counter and update according that the class.为什么不检查ajax调用计数器的值并根据该类进行更新。 This is an exemple of ajax callback.这是ajax回调的一个例子。

function (data) {
    if(data.counter == 0){
         $('#id').removeClass('the-class');
    }else{
         $('#id').addClass('the-class');
    }
}

 $(document).on("click", function () { var count_new = 0; var subtotal_new = 0; setTimeout(function () { jQuery.getJSON('/cart.js', function (data) { count_new = data.item_count; if (parseInt(count_new) == 0) { $(".Cart_click").find(".mobile-icons").find(".hotiya").removeClass("cart_ping") } else { $(".Cart_click").find(".mobile-icons").find(".hotiya").addClass("cart_ping") } }); }, 500); });

I devised a solution.我设计了一个解决方案。

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

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