You need to enable JavaScript to run this app. 如何用JavaScript按元素类名求和并计算表单表格的小计、折扣、总金额
优惠活动
大模型
产品
解决方案
定价
更多

如何用JavaScript按元素类名求和并计算表单表格的小计、折扣、总金额

解决方案

1. 先修正HTML表格结构

你原代码中每个商品的<td>没有包裹在<tr>标签内,会导致JS中closest('tr')无法正确获取父行,修正后的HTML代码如下:

<table id="tb">
  <tr>
    <td>1</td>
    <td>Shampoo de Zeolita</td>
    <td><input type="text" class="precio" name="precio1"  value="3500" style="color: black; background: transparent; border: 0; text-align: center;" readonly></td>
    <td><input type="number" class="cantidad" min="0" name="cantidad1" placeholder="0"></td>
    <td><input type="text" class="total" name="total1" placeholder="0" readonly></td>
  </tr>
  <tr>
    <td>2</td>
    <td>Shampoo de Almendras</td>
    <td><input type="text" class="precio" name="precio2" value="3500" style="color: black; background: transparent; border: 0; text-align: center;" readonly></td>
    <td><input type="number" class="cantidad" min="0" name="cantidad2" placeholder="0"></td>
    <td><input type="text" class="total" name="total2" placeholder="0" readonly></td>
  </tr>
  <tr>
    <td>3</td>
    <td>Shampoo de Manzanilla</td>
    <td><input type="text" class="precio" name="precio3"  value="7000" style="color: black; background: transparent; border: 0; text-align: center;" readonly></td>
    <td><input type="number" class="cantidad" min="0" name="cantidad3" placeholder="0"></td>
    <td><input type="text" class="total" name="total3" placeholder="0" readonly></td>
  </tr>
  <tr>
    <td colspan="4" align="right">Subtotal</td>
    <td><input type="text" class="subtotal" name="subtotal" placeholder="0" readonly></td>
  </tr>
  <tr>
    <td colspan="4" align="right">Descuento</td>
    <td><input type="text" class="descuento" name="descuento" placeholder="0" readonly></td>
  </tr>
  <tr>
    <td colspan="4" align="right">Total</td>
    <td><input type="text" class="totales" name="totales" placeholder="0" readonly></td>
  </tr>
</table>

我额外给所有计算结果字段加了readonly属性,避免用户手动修改导致数值错误,也修正了重复的name="total3"问题

2. 完整实现计算逻辑的JS代码

document.getElementById("tb").addEventListener("input", function(e) {
  // 先处理单行商品的总价计算
  const parent = e.target.closest("tr");
  // 只处理商品行,跳过小计/折扣/总行
  if (parent && parent.querySelector('.precio')) {
    const precio = Number(parent.querySelector('.precio').value) || 0;
    const cantidad = Number(parent.querySelector('.cantidad').value) || 0;
    const total = precio * cantidad;
    parent.querySelector('.total').value = total;
  }

  // 计算小计:遍历所有商品的total字段求和
  let subtotal = 0;
  document.querySelectorAll('.total').forEach(totalInput => {
    subtotal += Number(totalInput.value) || 0;
  });
  document.querySelector('.subtotal').value = subtotal;

  // 计算折扣,可根据需求调整区间和比例
  let descuento = 0;
  if (subtotal >= 150000 && subtotal <= 299999) {
    descuento = subtotal * 0.03; // 3%折扣
  } else if (subtotal >= 300000) { // 可以自行添加更多折扣区间
    descuento = subtotal * 0.05; // 示例:满30万打5%折扣
  }
  document.querySelector('.descuento').value = descuento;

  // 计算最终总金额
  const totalFinal = subtotal - descuento;
  document.querySelector('.totales').value = totalFinal;
});

逻辑说明

  • 每次输入数量后,先计算当前行的商品总价
  • 遍历所有商品行的总价累加得到小计
  • 按照你给出的折扣规则匹配对应折扣额,你可以直接修改代码中的区间数值和折扣比例适配实际业务需求
  • 用小计减去折扣额得到最终应付总金额

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.20 17:27:03