使用jquery计算插件求和

时间:2015-03-02 10:29:35

标签: jquery

如何知道表格中整个产品的总数量总和,以及如何在商品数量之前自动给出1,2,3之类的数字

任何建议都将受到赞赏

虚构>>

<table width="500">
                <col style="width: 50px;" />
                <col />
                <col style="width: 60px;" />
                <col style="width: 110px;" />
                <tr>
                    <th>
                        No #
                    </th>
                    <th>
                        Qty
                    </th>
                    <th align="left">
                        Product
                    </th>
                    <th>
                        Price
                    </th>
                    <th>
                        Total
                    </th>
                </tr>
                <tr>
                    <td>?</td>
                    <td align="center">
                        <input type="text" name="qty_item_1" id="qty_item_1" value="1" size="2" />
                    </td>
                    <td>
                        <a href="http://www.packtpub.com/jQuery/book">Learning jQuery</a>
                    </td>
                    <td align="center" id="price_item_1">
                        $39.99
                    </td>
                    <td align="center" id="total_item_1">
                        $39.99
                    </td>
                </tr>
                <tr>
                    <td>?</td>
                    <td align="center">
                        <input type="text" name="qty_item_2" id="qty_item_2" value="1" size="2" />
                    </td>
                    <td>
                        <a href="http://jquery.com/">jQuery Donation</a>
                    </td>
                    <td align="center" id="price_item_2">
                        $14.99
                    </td>
                    <td align="center" id="total_item_2">
                        $14.99
                    </td>
                </tr>
                <tr>
                    <td>Total QTY</td>
                    <td>?</td>

                    <td colspan="3" align="right">
                        <strong>Grand Total:</strong>
                    </td>
                    <td align="center" id="grandTotal">
                    </td>
                </tr>
            </table>

由于

热烈

1 个答案:

答案 0 :(得分:0)

recalc函数中尝试此操作:

var qtyTotals = 0;
$("input[name^=qty_item_]").each(function () {
    qtyTotals += parseInt($(this).val(), 10);
});
$("#qtyTotal").text(qtyTotals);

虚构http://jsfiddle.net/0e87jvor/2/