由类不工作定义的Jquery .each() - 显示第一个元素,但不显示其余元素

时间:2016-07-25 00:38:03

标签: jquery html class html-table each

我有下表,其中包含购物车中的商品

<table border='1'>
<thead>

<tr>
<th colspan='3'>
<strong id='jcart-title'>Shopping Cart</strong> (3 Items)
</th>
</tr>

</thead>
<tfoot>

<tr>
<th colspan='3'>
<input type='submit'  id='jcart-checkout' name='jcartCheckout' class='jcart-button' value='checkout' style='display:none' />
<span id='jcart-subtotal'>Before Tax Subtotal: <strong>$9.97</strong></span>
</th>
</tr>

</tfoot>
<tbody>

<tr>
<td class='jcart-item-qty' style='background:#FFF;'>
<input name='jcartItemId[]' type='hidden' value='63927746787' />
<input class='qty' id='jcartItemQty-63927746787' name='jcartItemQty[]' size='2' type='text' value='1' />
</td>
<td class='jcart-item-name' style='background:#FFF;'>
MARTINI OLIVES - STUFFED OLIVES - 5oz
<input name='jcartItemName[]' type='hidden' value='MARTINI OLIVES - STUFFED OLIVES - 5oz' />
</td>
<td class='jcart-item-price' style='background:#FFF;'>
<span>$1.99</span><input class='price' name='jcartItemPrice[]' type='hidden' value='1.99' />
<a class='jcart-remove' href='?jcartRemove=63927746787'>remove</a>
</td>
</tr>

<tr>
<td class='jcart-item-qty' style='background:#FFF;'>
<input name='jcartItemId[]' type='hidden' value='FB' />
<input class='qty' id='jcartItemQty-FB' name='jcartItemQty[]' size='2' type='text' value='1' />
</td>
<td class='jcart-item-name' style='background:#FFF;'>
FLASK BAG - PLASTIC - 0 OZ
<input name='jcartItemName[]' type='hidden' value='FLASK BAG - PLASTIC - 0 OZ' />
</td>
<td class='jcart-item-price' style='background:#FFF;'>
<span>$1.99</span><input class='price' name='jcartItemPrice[]' type='hidden' value='1.99' />
<a class='jcart-remove' href='?jcartRemove=FB'>remove</a>
</td>

</tr>
<tr>
<td class='jcart-item-qty' style='background:#FFF;'>
<input name='jcartItemId[]' type='hidden' value='011403' />
<input class='qty' id='jcartItemQty-011403' name='jcartItemQty[]' size='2' type='text' value='1' />
</td>
<td class='jcart-item-name' style='background:#FFF;'>
SHAKER - SHAKER - 
<input name='jcartItemName[]' type='hidden' value='SHAKER - SHAKER - ' />
</td>
<td class='jcart-item-price' style='background:#FFF;'>
<span>$5.99</span><input class='price' name='jcartItemPrice[]' type='hidden' value='5.99' />
<a class='jcart-remove' href='?jcartRemove=011403'>remove</a>
</td>
</tr>

</tbody>
</table>

我想使用以下jquery为每个项目提取.qty和.price输入值。注意:我正在使用$(body).click(启动该功能。

$('body').click(function() {
var grandTotal = 0;
var qty = $('.qty').val();
var price = $('.price').val();
var subtot = qty * price;    $('tr').each(function (i) {

$('#printorder').html('QTY: ' + qty +', PRICE: '+ price +', TOTAL:' + subtot +'<br>');
});

我得到一行结果......数量:1,价格:1.99,总计:1.99但表格中有三个项目。有没有人有任何想法会出错?

1 个答案:

答案 0 :(得分:1)

您没有正确使用循环的上下文。此外,您多次设置输出的html。

试试这个:

$('body').click(function() {
var output = '';
$('tbody tr').each(function (i) {
    var qty = $(this).find('.qty').val();
    var price = $(this).find('.price').val();
    var subtot = qty * price;
    output += 'QTY: ' + qty +', PRICE: '+ price +', TOTAL:' + subtot +'<br>';
});
$('#printorder').html(output);

编辑;这是您的原始代码正在做的事情:

$('body').click(function() {
var grandTotal = 0; 
var qty = $('.qty').val(); // sets qty to val of first item in $('.qty') (runs once)
var price = $('.price').val(); // sets price to val of first item in $('.price') (runs once)
var subtot = qty * price; 
$('tr').each(function (i) { // loops over all <tr> tags, runs 5 times
    // sets $('#printorder') innerHtml with values calculated outside of loop (always the same value).
    // this happens 5 times with the same values, and each time overwrites the last
    $('#printorder').html('QTY: ' + qty +', PRICE: '+ price +', TOTAL:' + subtot +'<br>');
});