我想让我的加号和减号按钮更改小计和总价,但价格没有改变,任何人都可以解决我的问题吗?



这是我的代码,我试过累计价格与数量每件产品,但仍然卡住,谁可以解决我的问题?我需要你们的帮助,谢谢你们尝试我的代码

<td>
{{-- <input type="number" value="2" class="form-control input-kuantitas"> --}}
<div class="wrapper">
<span class="minus">-</span>
<span class="num" id="quantity_{{ $pesanan->id }}">1</span>
<span class="plus">+</span>
</div>
</td>

<td id="price_{{ $pesanan->id }}">{{ $pesanan->harga }}</td>
<div class="row">
<div class="col-md-4 ms-auto">
<div class="table-responsive">
<table class="table">
<tr>
<th>Total</th>
<td id="total">Rp. {{ $harga }}</td>
</tr>
<script>
$('.plus').click(function (e) { 
const elem=$(this).prev();
let qty = parseInt(elem.html())+1;
elem.html(qty);
});
$('.minus').click(function (e) { 
const elem=$(this).next();
let qty = parseInt(elem.html());
if(qty>1){
qty--;
}
elem.html(qty);
});
</script>

很难假设'total'是如何计算的,因为上面的场景中没有给出。

试试这个

<script>
$('.plus').click(function (e) { 
const elem=$(this).prev();
const getId=elem.attr("id").split("_")[1]; // To find the price id
const price=parseFloat($("#price_"+getId).text()); // price amount
let qty = parseInt(elem.html())+1;
elem.html(qty);
$("#total").html("Rp. "+price*qty); // set total, assume total is qty * price
});
$('.minus').click(function (e) { 
const elem=$(this).next();
const getId=elem.attr("id").split("_")[1]; // To find the price id
const price=parseFloat($("#price_"+getId).text()); // price amount
let qty = parseInt(elem.html());
if(qty>1){
qty--;
}
elem.html(qty);
$("#total").html("Rp. "+price*qty); // set total, assume total is qty * price
});
</script>

希望这段代码对你有帮助。

相关内容

最新更新