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 12:06:12 浏览: 66
您可以在代码中添加以下内容来设置未点击时小计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。
相关问题
for (var i = 0; i < a.length;i++)
This for loop is missing an increment statement for the variable i. It should be:
for (var i = 0; i < a.length; i++)
The increment statement (i++) will ensure that the loop continues to run until the condition (i < a.length) is no longer true.
//点加,小计 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) > 1) { var newvalue = parseInt(q.value) - 1; q.setAttribute("value", newvalue); s.textContent = (newvalue * parseFloat(p)).toFixed(2); calculateTotal(); } }; }根据此代码设初始数量为1
如果需要将初始数量设为1,可以在HTML代码中将数量的输入框的value值设置为1,或者在JavaScript代码中将数量的输入框的value属性设置为1,例如:
```javascript
var quantity = document.getElementsByClassName("quantity");
for (var i = 0; i < quantity.length; i++) {
quantity[i].value = 1;
}
```
这样就可以将所有数量的输入框初始值设置为1。
阅读全文