输入复选框仅更新一个值,但其他输入不更新?
因此,我在计算总计菜单项的地方制作了这个应用程序,其中包括$ 5的交货费。问题是第四个选项仅包括总计5美元的费用,但其他3个选项不包括
此处的费用是我的codepen https://codepen.io/shodoro/shodoro/pen/pen/dydnopx
冰沙$ 4,唯一可以正确添加交货费的输入复选框?
前3个选项不包括总计5美元的交货费,我不知道该如何修复
这是我的JS,
function updatePrice() {
let items = 0;
let deliveryFee = document.getElementById('fee')
let tax = document.getElementById('tax')
let tip = document.getElementById('tip')
tax = .1
tip = .2
document.querySelectorAll('input[type=checkbox]').forEach(checkBox => {
if (checkBox.checked) {
items += +checkBox.value
deliveryFee = 5
} else {
deliveryFee = 0
}
})
document.getElementById("price").textContent = `Food Total: $${(items).toFixed(2)}`;
document.getElementById("tax").textContent = `Tax (10%): $${(items * tax).toFixed(2)}`;
document.getElementById("tip").textContent = `Tip (20%): $${(items * tip).toFixed(2)}`;
document.getElementById("total").textContent = `Your order total is: $${((items * tax)+(items * tip)+(items)+(deliveryFee)).toFixed(2)}`;
}
我希望所有选项在单击它们时都包含送货费,还可以确保重置送货费每当您取消选中所有选项时,要0。
So I have this app I made where it calculates the menu items total and includes a $5 delivery fee. The problem is the 4th option only includes the $5 fee in the total, but the other 3 options don't include the fee
Here's my codepen
https://codepen.io/shodoro/pen/dydNopX
Why is my 4th option, the smoothie $4 the only input checkbox that adds the delivery fee correctly?
The first 3 options don't include the $5 delivery fee in the total and I don't know how to fix it
Here's my JS
function updatePrice() {
let items = 0;
let deliveryFee = document.getElementById('fee')
let tax = document.getElementById('tax')
let tip = document.getElementById('tip')
tax = .1
tip = .2
document.querySelectorAll('input[type=checkbox]').forEach(checkBox => {
if (checkBox.checked) {
items += +checkBox.value
deliveryFee = 5
} else {
deliveryFee = 0
}
})
document.getElementById("price").textContent = `Food Total: ${(items).toFixed(2)}`;
document.getElementById("tax").textContent = `Tax (10%): ${(items * tax).toFixed(2)}`;
document.getElementById("tip").textContent = `Tip (20%): ${(items * tip).toFixed(2)}`;
document.getElementById("total").textContent = `Your order total is: ${((items * tax)+(items * tip)+(items)+(deliveryFee)).toFixed(2)}`;
}
Essential I want all options to include the delivery fee when clicking them, but also making sure the delivery fee resets to 0 whenever you uncheck all options.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那是因为您正在设置
deliveryfee
在循环中,因此,例如,如果检查了12件机翼项目,则将其设置为deliveryfee
至5,然后将其设置为循环直到下一个项目(6件机翼),它将设置deliveryfee
为0。 5。我认为也许您想要更多这样的东西:That's because you are setting
deliveryFee
in a loop and so if, for example, the 12 piece wings item is checked, then it setsdeliveryFee
to 5 and then it's going to loop through to the next item (6 piece wings) and it will setdeliveryFee
to 0. So when it gets to the calculation for the total, itdeliveryFee
will be 0 and not 5. I think maybe you want something more like this: