You need to enable JavaScript to run this app. jQuery实现表格行值相乘核算行价与总计仅首行生效问题求解
优惠活动
大模型
产品
解决方案
定价
更多

jQuery实现表格行值相乘核算行价与总计仅首行生效问题求解

问题排查与修复方案

核心失效原因

  • 根本触发失效原因:HTML规范要求页面内id全局唯一,当前Blade循环渲染时,给每一行的减按钮、数量输入框、加按钮都绑定了完全相同的id(#decrease、#quantity、#increment)。jQuery的id选择器只会匹配DOM中第一个出现的对应元素,后续行的同id元素完全无法被选中,自然绑定不上事件,这也是只有第一行能正常触发计算的核心原因。
  • 全表总价计算逻辑错误:原代码中的mult变量未做初始化,且采用“触发一次就叠加一次当前行总价”的逻辑,没有在每次变更后重新遍历全表累加所有行总价,会导致总价重复累加、数值偏移;甚至输入值调小/清空时,总价不会对应扣减。
  • 表单字段名重复:循环内的数量输入框统一使用name="quantity",没有带行标识,表单提交时只会覆盖保留最后一行的输入值,后端无法正确获取每行数据。
  • 按钮事件逻辑割裂:按钮上的原生行内onclick仅负责修改输入框数值,没有联动触发外层绑定的计算逻辑。

修复实现

第一步:调整Blade模板渲染规则

移除循环内所有重复的全局id,改用统一类名做选择标识,给表单字段加行级唯一标识,把单价存在自定义data属性中方便JS读取:

@foreach($service->invoices as $invoice)
    <tr>
        <td class="text-right">{{ $invoice->description }}</td>
        <td>
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" name="custom{{ $invoice->id }}" id="custom{{ $invoice->id }}">
                <label class="custom-control-label" for="custom{{ $invoice->id }}"></label>
            </div>
        </td>
        <td>
            <div class="form-row justify-content-center">
                <div class="form-group mb-0">
                    <div class="input-group mx-auto mb-0">
                        <div class="number-input amount">
                            <!-- 移除重复id,加统一类名做选择 -->
                            <button type="button" class="quantity-btn quantity-decrease">-</button>
                            <input 
                                class="quantity bg-light" 
                                min="0" 
                                placeholder="0" 
                                name="quantity[{{ $invoice->id }}]" 
                                value="0" 
                                type="number"
                            >
                            <button type="button" class="quantity-btn quantity-increase plus">+</button>
                        </div>
                    </div>
                </div>
            </div>
        </td>
        <!-- 把单价存在data属性中,避免文本解析问题 -->
        <td class="price" data-price="{{ $invoice->price }}">{{ $invoice->price }}</td>
        <td class="total">0.00</td>
    </tr>
@endforeach

<!-- 页面中原有展示全表总价的元素保持不变 -->
<span id="grandTotal">0.00</span>

第二步:调整jQuery计算逻辑

改用类选择器匹配所有行的触发元素,采用事件委托兼容后续可能动态加载的行;每次计算先更新当前行总价,再重新遍历所有行累加得到全表总价,避免累加偏移:

<script>
// 统一绑定触发事件:匹配所有数量输入框、加减按钮
$(document).on("input click", ".quantity, .quantity-btn", function() {
    const currentRow = $(this).closest('tr');
    
    // 校验当前行输入值,不允许负数
    let currentQuantity = parseFloat(currentRow.find('.quantity').val()) || 0;
    currentQuantity = Math.max(0, currentQuantity);
    currentRow.find('.quantity').val(currentQuantity);

    // 计算当前行总价
    const unitPrice = parseFloat(currentRow.find('.price').data('price')) || 0;
    const rowTotal = unitPrice * currentQuantity;
    currentRow.find('.total').text(rowTotal.toFixed(2));

    // 重新遍历全表行总价,累加得到最终全表总价
    let grandTotal = 0;
    $('table .total').each(function() {
        grandTotal += parseFloat($(this).text()) || 0;
    });
    $("#grandTotal").text(grandTotal.toFixed(2));
})
</script>

实现说明:调整后不管是手动在输入框填数、点击加减按钮,所有行的计算逻辑都会正常触发,全表总价也会跟随每行的数值变更实时更新,不会出现之前的累加错误问题。


内容的提问来源于stack exchange,提问作者user13979489

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.25 23:45:03