【问题标题】:Jquery .each() defined by class not working - displays first element but not the rest由类定义的 Jquery .each() 不起作用 - 显示第一个元素但不显示其余元素
【发布时间】:2016-11-28 05:38:13
【问题描述】:

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

<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,但表中有三个项目。有谁知道出了什么问题?

【问题讨论】:

    标签: jquery html class html-table each


    【解决方案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>');
    });
    

    【讨论】:

    • 非常感谢您的解释。它是如何工作的,因为它在我发布之前阅读的其他类似问题中并不那么清楚。 $(this) 是我现在理解 each() 函数如何工作的重要部分。再次感谢,如果不是你的例子,我会在理解这个问题上花费更多的时间。
    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 2022-06-14
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多