[英]Adding quantity of shopping cart table rows items
I have the following markup in foreach
: 我在
foreach
有以下标记:
@foreach($addedToCart as $item)
<tr class="cart-row">
<td>{{ $item->product->name }}</td>
<td>{{ $item->product->price }}</td>
<td>
<div class="value-button" id="decrease" onclick="decreaseValue()" value="Decrease Value">-</div>
<input type="text" id="number" class="totalQuantity" value="1" />
<div class="value-button" id="increase" class="increase" data-unit-price="{{ $item->product->price }}" onclick="increaseValue()" value="Increase Value" data-value="1">+</div>
</td>
</tr>
@endforeach
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<h3>Total Amount : $ <span id="totalAmount"> 0</span></h3>
</div>
Jquery : jQuery的:
$('#increase').on("click", function () {
var st = 0;
$('.cart-row').each(function () {
var i = $('#increase', this);
var up = $(i).data('unit-price');
var q = $('.totalQuantity').val();
st = st + (up * q);
});
$('#totalAmount').text(st);
});
The issue is that, It only works for only first row. 问题是,它仅适用于第一行。 The
<tr></tr>
is in foreach
and every tr have class cart-row
. <tr></tr>
在foreach
,每个tr都有类cart-row
。
What am i missing ? 我想念什么?
Its working on 1st element only because there must be only one ID
in a DOM. 它仅对第一个元素起作用,因为DOM中必须只有一个
ID
。 If you put more than one then it will consider the 1st one from top in the DOM. 如果您放置多个,则它将考虑DOM中自上而下的第一个。
From the code you have posted, I can see that you have also binded class
with the same name. 从您发布的代码中,我可以看到您还绑定了具有相同名称的
class
。 So if you use it as a selector in your jquery code it will work fine. 因此,如果将其用作jquery代码中的选择器,它将可以正常工作。
$('.increase').on("click", function () {
var st = 0;
$('.cart-row').each(function (i, obj) {
var up = $('.cart-row:eq('+i+')').find('.increase').data('unit-price');
var q = $('.cart-row:eq('+i+')').find('.totalQuantity').val();
st = st + (up * q);
});
$('#totalAmount').text(st);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.