HTML代码:
html>
A03号桌菜品名称数量单价价格
大闸蟹+
1
-
¥68.00
¥68.00
在天愿作比翼鸟+
1
-
¥68.00
¥68.00
红嘴绿鹦哥+
1
-
¥68.00
¥68.00
合计:¥242.00
保存
JS代码:
/****点击增加按钮****/
$('.add').click(function(){
//修改数量
var n=$(this).next().html();
var num=parseInt(n)+1;
$(this).next().html(num);
//计算价格
var c= $(this).parent().siblings().children('.price').html();
parseInt(c);
var subPrice = num * c;
var sub_price = subPrice.toFixed(2); //保留小数点后面两位小数
$(this).parent().siblings().children('.sub_total').html(sub_price);
//计算总价
var total=0;
$('.sub_total').each(function(){
var price=parseInt($(this).html());
total+=price;
var total_price = total.toFixed(2);
$('.total b').html(total_price);
});
});
/****点击减少按钮****/
$('.reduce').click(function(){
//修改数量
var n=$(this).prev().html();
var num=parseInt(n)-1;
if(num==0){return;}//数量减到0就能再减了
$(this).prev().html(num);
//计算价格
var c= $(this).parent().siblings().children('.price').html();
parseInt(c);
var subPrice = num * c;
subPrice.toFixed(2);
var sub_price = subPrice.toFixed(2);
$(this).parent().siblings().children('.sub_total').html(sub_price);
//计算总价
var total=0;
$('.sub_total').each(function(){
var price=parseInt($(this).html());
total+=price;
var total_price = total.toFixed(2);
$('.total b').html(total_price);
});
});
考虑到篇幅问题,没有贴出CSS代码,最终页面截图如下: