当前位置:首页 > JavaScript

js实现小计

2026-02-01 09:55:48JavaScript

实现小计功能

在JavaScript中实现小计功能通常涉及对一组数据进行累加或汇总。以下是几种常见场景的实现方法:

数组元素求和

const numbers = [1, 2, 3, 4, 5];
const subtotal = numbers.reduce((sum, num) => sum + num, 0);
console.log(subtotal); // 输出15

表格行小计

假设有一个HTML表格,需要计算每行的小计:

<table id="order-table">
  <tr>
    <td><input type="number" class="quantity" value="2"></td>
    <td><input type="number" class="price" value="10"></td>
    <td class="subtotal">20</td>
  </tr>
  <tr>
    <td><input type="number" class="quantity" value="3"></td>
    <td><input type="number" class="price" value="5"></td>
    <td class="subtotal">15</td>
  </tr>
</table>
<p>总计: <span id="total">0</span></p>
function calculateSubtotals() {
  const rows = document.querySelectorAll('#order-table tr');
  let grandTotal = 0;

  rows.forEach(row => {
    const quantity = parseFloat(row.querySelector('.quantity').value) || 0;
    const price = parseFloat(row.querySelector('.price').value) || 0;
    const subtotal = quantity * price;
    row.querySelector('.subtotal').textContent = subtotal.toFixed(2);
    grandTotal += subtotal;
  });

  document.getElementById('total').textContent = grandTotal.toFixed(2);
}

// 监听输入变化
document.querySelectorAll('#order-table input').forEach(input => {
  input.addEventListener('change', calculateSubtotals);
});

// 初始计算
calculateSubtotals();

对象数组属性汇总

处理包含多个对象的数组时,可以这样计算小计:

const items = [
  { name: 'Item1', quantity: 2, price: 10 },
  { name: 'Item2', quantity: 1, price: 20 },
  { name: 'Item3', quantity: 3, price: 5 }
];

const subtotals = items.map(item => ({
  ...item,
  subtotal: item.quantity * item.price
}));

const grandTotal = subtotals.reduce((sum, item) => sum + item.subtotal, 0);
console.log(subtotals, grandTotal);

动态添加项目的小计

对于动态添加的项目,可以使用事件委托来处理计算:

js实现小计

document.getElementById('item-container').addEventListener('input', (e) => {
  if (e.target.classList.contains('quantity') || e.target.classList.contains('price')) {
    const row = e.target.closest('.item-row');
    const quantity = parseFloat(row.querySelector('.quantity').value) || 0;
    const price = parseFloat(row.querySelector('.price').value) || 0;
    row.querySelector('.subtotal').textContent = (quantity * price).toFixed(2);
    updateGrandTotal();
  }
});

function updateGrandTotal() {
  const subtotals = Array.from(document.querySelectorAll('.subtotal'));
  const total = subtotals.reduce((sum, el) => sum + parseFloat(el.textContent), 0);
  document.getElementById('grand-total').textContent = total.toFixed(2);
}

这些方法涵盖了JavaScript中实现小计功能的常见场景,可以根据具体需求选择或组合使用。

标签: 小计js
分享给朋友:

相关文章

js实现轮播图

js实现轮播图

基础轮播图实现 使用HTML、CSS和JavaScript实现一个简单的自动轮播图。HTML结构包含一个容器和多个图片项。 <div class="slider"> <div…

js实现验证

js实现验证

验证表单输入 使用JavaScript验证表单输入是常见的需求。可以通过监听表单提交事件,检查输入字段是否符合要求。 document.getElementById('myForm').addEve…

jquery.js

jquery.js

jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画设计和 Ajax 交互等操作。以下是关于 jQuery.js 的核心信息和使用方法: 获取 jQ…

js实现换肤

js实现换肤

使用CSS变量实现换肤 通过CSS变量可以轻松实现主题切换功能。CSS变量在根元素中定义,通过JavaScript动态修改这些变量值。 :root { --primary-color: #349…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。…

js树实现

js树实现

树的基本概念 树是一种非线性的数据结构,由节点和边组成。每个节点包含一个值和指向子节点的引用。树的顶部节点称为根节点,没有子节点的节点称为叶节点。 树的实现方式 在JavaScript中,树可以通过…