for (var i = 0; i < increment.length; i++) { increment[i].index = i; increment[i].onclick = function() { var flag = this.index; var q = quantity[flag]; var p = price[flag].textContent; var s = subtotal[flag]; var newvalue = parseInt(q.value) + 1; q.setAttribute("value", newvalue); s.textContent = (newvalue * parseFloat(p)).toFixed(2); calculateTotal(); }; } //点减,小计 for (var j = 0; j < decrement.length; j++) { decrement[j].index = j; decrement[j].onclick = function() { var flag = this.index; var q = quantity[flag]; var p = price[flag].textContent; var s = subtotal[flag]; if (parseInt(q.value) > 0 ) { var newvalue = parseInt(q.value) - 1; q.setAttribute("value", newvalue); s.textContent = (newvalue * parseFloat(p)).toFixed(2); calculateTotal(); } }; } function calculateTotal() { var total = 0.0; for (var k = 0; k < subtotal.length; k++) { total += parseFloat(subtotal[k].textContent); } document.getElementById("total").textContent = total.toFixed(2); }根据此代码设置未点击时小计subtotal和总价total为0.00
时间: 2024-02-10 14:06:12 浏览: 61
数据结构中的各种排序方法小结(JS实现)
您可以在代码中添加以下内容来设置未点击时小计subtotal和总价total为0.00:
```javascript
// 在页面加载时将小计和总价设置为0.00
window.onload = function() {
for (var k = 0; k < subtotal.length; k++) {
subtotal[k].textContent = "0.00";
}
document.getElementById("total").textContent = "0.00";
};
```
这段代码将在页面加载时运行,将所有的小计设置为0.00,并将总价设置为0.00,以确保在用户还没有开始点击加减按钮时,小计和总价都是0.00。
阅读全文