我正在尝试让添加到购物车按钮更改下面的价格,将有多个产品,他们都需要加总价格。我已经写出了jQuery,但有一些问题,目标价格放入购物车。
任何帮助都将不胜感激!
https://codepen.io/anon/pen/adOXjM
产品的HTML:
<div class="product">
<div class="product-img">
<img src="http://cdn-images.farfetch.com/11/12/88/52/11128852_5407427_1000.jpg">
</div>
<div class="product-actions">
<div class="price">
$<span>97</span>
</div>
<button class="add-to-cart">Add to Cart</button>
<span class="add-product">+</span>
<span class="minus-product">-</span>
</div>
</div>
jQuery:
var price = 0;
$('add-to-cart').click(function(){
$(this).toggleClass('highlight');
if($(this).hasClass('highlight')){
price += parseInt($(this).parent().child().find('.price > span').text());
}else{
price -= parseInt($(this).parent().find('.price > span').text());
}
$('.shopping-cart > span').text(price);
});
codepen将给出所有的细节和清晰的视图。
非常感谢!
发布于 2015-12-11 11:53:36
$('.hamburger').click(function() {
$('.nav-menu').toggleClass("nav-show");
$('.hamburger').toggleClass("hamburger-click");
});
var price = 0;
$('.add-to-cart').click(function(){
$(this).toggleClass('highlight');
if($(this).hasClass('highlight')){
price += parseInt($(this).parent().find('.price > span').text());
}else{
price -= parseInt($(this).parent().find('.price > span').text());
}
$('.shopping-cart > span').text(price);
});
$('add-to-cart')
缺少类选择器(.)
$('.add-to-cart')
和
price += parseInt($(this).parent().child().find('.price > span').text());
需要删除child()
price += parseInt($(this).parent().find('.price > span').text());
https://stackoverflow.com/questions/34215458
复制相似问题