JavaScript-Checkbox循环未正确总计价格 [英] JavaScript - Checkbox loop not totalling up prices correctly

查看:32
本文介绍了JavaScript-Checkbox循环未正确总计价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击顶部的复选框时,它会在总计框中放置一个"0",因此我知道它已正确连接,但是我认为循环逻辑中存在问题.html中的元素之一如下所示.

When I click on the checkbox at the top, it puts a '0' in the total box, so I know that it is connected correctly, however I think there is a problem in the logic in the loop. One of the elements in html looks like this.

const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
const checkboxes = document.querySelectorAll('input[data-price][type=checkbox]');
const cbamount = checkboxes.length;

document.getElementsByName('event[]')[0].onclick = function() {
  totalPrice()
};

function totalPrice() {
  let totalprice = 0;
  for (let i = 0; i < cbamount; i++) {
    const box = checkboxes[i];
    if (box.checked) {
      box.dataset.price = totalprice + box.dataset.price;
    } //if
  } //for

  document.getElementsByName("total")[0].value = totalprice;
}

<span class="eventTitle">Carmen </span>
<span class="eventStartDate">2020</span>
<span class="eventEndDate">2020</span>
<span class="catDesc">T</span>
<span class="venueName">Mill </span>
<span class="eventPrice">3</span>
<span class="chosen"><input type="checkbox" name="event[]" value="11" data-price="35.00"></span>


<section id="Cost">
<h3>Total</h3>
	Total <input type="text" name="total" size="20" readonly="">
</section>

推荐答案

您提供的代码中没有 total .

当只有一个元素时,我个人会使用ID;如果有更多元素,我会使用相对地址和/或委托

I would personally use ID when only having one element and if more, use relative addressing and/or delegation

const form = document.getElementById('booking');
const total = document.getElementById('total');
document.getElementById("booking").addEventListener("click", function(e) {
  if (e.target.name === "event[]") {
    let totalprice = 0;
    [...document.querySelectorAll('input[data-price][type=checkbox]')].forEach(function(box) {
      if (box.checked) {
        totalprice += +box.dataset.price;
      } //if
    })
    document.querySelector("[name=total]").value = totalprice.toFixed(2);
  }
})

<form id="booking" method="get">
  <section id="book">
    <h2>Select Events</h2>

    <div class="item">
      <span class="eventTitle">Carmen </span>
      <span class="eventStartDate">2020</span>
      <span class="eventEndDate">2020</span>
      <span class="catDesc">T</span>
      <span class="venueName">Mill </span>
      <span class="eventPrice">3</span>
      <span class="chosen"><input name="event[]" type="checkbox" value="11" data-price="35.00"></span>
    </div>
    <div class="item">
      <span class="eventTitle">Ash</span>
      <span class="eventStartDate">202</span>
      <span class="eventEnd">2020-12-31</span>
      <span class="catD">Exhib</span>
      <span class="venueNa">The Biy</span>
      <span class="eventPr">0.00</span>
      <span class="chosen"><input type="checkbox" name="event[]" value="17" data-price="10.00"></span>
    </div>
  </section>

  <section id="Cost">
    <h3>Total</h3>
    Total <input type="text" name="total" size="20" readonly="">
  </section>
</form>

这篇关于JavaScript-Checkbox循环未正确总计价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆