0
請幫我解決我的問題。下面的代碼正常工作,但問題是數據僅從第一次迭代中獲取。我如何讓它適用於所有人?如果問題非常愚蠢,我很抱歉。jquery。每個動態多個值
<tbody><tr class="tr_cart_1">
<input type="hidden" value="1" id="item_id_cart">
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img
src="..." width="100" height="100" alt="..."
class="img-responsive" /></div>
<div class="col-sm-10">
<h4 class="nomargin">...</h4>
<p>...</p>
</div>
</div>
</td>
<td data-th="Price"><span id="price_cart">123</span></td>
<td data-th="Quantity">
<form action="" method="post" name="refresh" class="text-center"><div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value="..."></div>
<input id="qty_cart" name="qty" size="2" type="text" value="2"></form>
</td>
<td data-th="Subtotal" class="text-center">
<span class="sum_cart" id="id_sum_cart_1">246</span>
</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm" id="ref_cart"><i class="fa fa-refresh"></i></button>
<button class="btn btn-danger btn-sm" id="del_cart"><i class="fa fa-trash-o"></i></button>
</td>
</tr><tr class="tr_cart_3">
<input type="hidden" value="3" id="item_id_cart">
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img
src="..." width="100" height="100" alt="..."
class="img-responsive" /></div>
<div class="col-sm-10">
<h4 class="nomargin">...</h4>
<p>...</p>
</div>
</div>
</td>
<td data-th="Price"><span id="price_cart">30</span></td>
<td data-th="Quantity">
<form action="" method="post" name="refresh" class="text-center"><div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value=""></div>
<input id="qty_cart" name="qty" size="2" type="text" value="2"></form>
</td>
<td data-th="Subtotal" class="text-center">
<span class="sum_cart" id="id_sum_cart_3">60</span>
</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm" id="ref_cart"><i class="fa fa-refresh"></i></button>
<button class="btn btn-danger btn-sm" id="del_cart"><i class="fa fa-trash-o"></i></button>
</td>
</tr><tr class="tr_cart_2">
<input type="hidden" value="2" id="item_id_cart">
<td data-th="Product">
<div class="row">
<div class="col-sm-2 hidden-xs"><img
src="..." width="100" height="100" alt="..."
class="img-responsive" /></div>
<div class="col-sm-10">
<h4 class="nomargin">...</h4>
<p></p>
</div>
</div>
</td>
<td data-th="Price"><span id="price_cart">30</span></td>
<td data-th="Quantity">
<form action="" method="post" name="refresh" class="text-center"><div style="display:none;"><input id="csrf_token" name="csrf_token" type="hidden" value=""></div>
<input id="qty_cart" name="qty" size="2" type="text" value="2"></form>
</td>
<td data-th="Subtotal" class="text-center">
<span class="sum_cart" id="id_sum_cart_2">60</span>
</td>
<td class="actions" data-th="">
<button class="btn btn-info btn-sm" id="ref_cart"><i class="fa fa-refresh"></i></button>
<button class="btn btn-danger btn-sm" id="del_cart"><i class="fa fa-trash-o"></i></button>
</td>
</tr></tbody>
$(document).on("click", "#ref_cart_btn", function() {
$('[class*="tr_cart_"]').each(function(i) {
var item_id_cart = $("#item_id_cart").attr('value');
var qty = $("#qty_cart").val();
alert (qty);
var price = $("#price_cart").text();
alert (price);
alert(item_id_cart);
$("#id_sum_cart_" + item_id_cart).text(qty*price);
});
});
它的工作原理,TY非常感謝!很有幫助。 –