输入复选框仅更新一个值,但其他输入不更新?

发布于 2025-01-28 23:49:07 字数 1412 浏览 2 评论 0原文

因此,我在计算总计菜单项的地方制作了这个应用程序,其中包括$ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2025-02-04 23:49:07

那是因为您正在设置deliveryfee在循环中,因此,例如,如果检查了12件机翼项目,则将其设置为deliveryfee至5,然后将其设置为循环直到下一个项目(6件机翼),它将设置deliveryfee为0。 5。我认为也许您想要更多这样的东西:

          function updatePrice() {
              console.log('updatePrice');
              let items = 0;
              let deliveryFee = 0;
              let tax = document.getElementById('tax')    
              let tip = document.getElementById('tip')

              tax = .1
              tip = .2
              console.log('before forEach loop', items, deliveryFee);
              document.querySelectorAll('input[type=checkbox]').forEach(checkBox => {
                  console.log(checkBox);
                  if (checkBox.checked) {
                      console.log('checked!')
                      items += +checkBox.value
                      if (deliveryFee == 0) {
                        console.log('First checked item, setting delivery fee to 5.')
                        deliveryFee = 5;
                      }
                  } else {
                    console.log('not checked!');
                  }
              })
              console.log('after forEach loop', items, deliveryFee);
            
              if (items >= 10) {
                deliveryFee = deliveryFee * 2;
              }

              let orderTotal = (items * tax)+(items * tip)+(items) + deliveryFee;

              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("fee").textContent = `Delivery Fee: ${deliveryFee.toFixed(2)}`;
              document.getElementById("total").textContent = `Your order total is: ${orderTotal}`;
          }

That's because you are setting deliveryFee in a loop and so if, for example, the 12 piece wings item is checked, then it sets deliveryFee to 5 and then it's going to loop through to the next item (6 piece wings) and it will set deliveryFee to 0. So when it gets to the calculation for the total, it deliveryFee will be 0 and not 5. I think maybe you want something more like this:

          function updatePrice() {
              console.log('updatePrice');
              let items = 0;
              let deliveryFee = 0;
              let tax = document.getElementById('tax')    
              let tip = document.getElementById('tip')

              tax = .1
              tip = .2
              console.log('before forEach loop', items, deliveryFee);
              document.querySelectorAll('input[type=checkbox]').forEach(checkBox => {
                  console.log(checkBox);
                  if (checkBox.checked) {
                      console.log('checked!')
                      items += +checkBox.value
                      if (deliveryFee == 0) {
                        console.log('First checked item, setting delivery fee to 5.')
                        deliveryFee = 5;
                      }
                  } else {
                    console.log('not checked!');
                  }
              })
              console.log('after forEach loop', items, deliveryFee);
            
              if (items >= 10) {
                deliveryFee = deliveryFee * 2;
              }

              let orderTotal = (items * tax)+(items * tip)+(items) + deliveryFee;

              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("fee").textContent = `Delivery Fee: ${deliveryFee.toFixed(2)}`;
              document.getElementById("total").textContent = `Your order total is: ${orderTotal}`;
          }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文