英文:
How to prevent checkbox loop and radio button loop from creating two separate price totals
问题
我有一个选择复选框的查询选择器,它运行良好,并且我为单选按钮复制了它,但我遇到了一个问题,它们都保持独立的总数。我尝试将totalPrice变量从两者中取出并放在外部,以便它可以共享,但这会导致未选中的复选框和单选按钮的价格未被移除。
这是我的当前有问题的代码:
const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
document.getElementById("bookingForm").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;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
document.getElementById("bookingForm").addEventListener("click", function(e) {
if (e.target.name === "delivery") {
let totalprice = 0;
[...document.querySelectorAll('input[data-price][type=radio]')].forEach(function(box) {
if (box.checked) {
totalprice += +box.dataset.price;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
<form id="bookingForm" action="javascript:alert('form submitted');" method="get">
<section id="bookEvents">
<h2>Select Events</h2>
<div class='item'>
<span class='eventTitle'>Event number 1</span>
<span class='eventPrice'>10.50</span>
<span class='chosen'><input type='checkbox' name='event[]' value='1' data-price='10.50'></span>
</div>
<div class='item'>
<span class='eventTitle'>Event number 2</span>
<span class='eventPrice'>5.00</span>
<span class='chosen'><input type='checkbox' name='event[]' value='2' data-price='5.00'></span>
</div>
<section id="Cost">
<input type="radio" name="delivery" value="ticket" data-price="10">
<h2>Total Price</h2>
Total Price <input type="text" name="total" size="12">
</section>
<p><input type="submit" name="submit" value="Book"></p>
</section>
</form>
请注意,我已经将两个事件监听器合并成一个以避免问题。
英文:
I have a query selector for checkboxes which works fine and I replicated it for radio button, but I am running into the problem that they are both keeping separate totals. I tried to take the totalPrice variable out of both of them and place it outside so that it would be shared, but that created the problem of the price not being removed from unchecked checkboxes and radio buttons.
This is my current buggy code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
document.getElementById("bookingForm").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;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
document.getElementById("bookingForm").addEventListener("click", function(e) {
if (e.target.name === "delivery") {
let totalprice = 0;
[...document.querySelectorAll('input[data-price][type=radio]')].forEach(function(box) {
if (box.checked) {
totalprice += +box.dataset.price;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
<!-- language: lang-html -->
<form id="bookingForm" action="javascript:alert('form submitted');" method="get">
<section id="bookEvents">
<h2>Select Events</h2>
<div class='item'>
<span class='eventTitle'>Event number 1</span>
<span class='eventPrice'>10.50</span>
<span class='chosen'><input type='checkbox' name='event[]' value='1' data-price='10.50'></span>
</div>
<div class='item'>
<span class='eventTitle'>Event number 2</span>
<span class='eventPrice'>5.00</span>
<span class='chosen'><input type='checkbox' name='event[]' value='2' data-price='5.00'></span>
</div>
<section id="Cost">
<input type="radio" name="delivery" value="ticket" data-price="10">
<h2>Total Price</h2>
Total Price <input type="text" name="total" size="12">
</section>
<p><input type="submit" name="submit" value="Book"></p>
</section>
</form>
<!-- end snippet -->
答案1
得分: 1
只需一个事件处理程序和一个循环
如果你只在需要循环的元素上有 data-price,那么你可以将以下代码:
[...document.querySelectorAll('input[data-price][type=checkbox], input[data-price][type=radio]')].forEach(function(box) {
更改为:
[...document.querySelectorAll('input[data-price]')].forEach(function(box) {
我只是想知道为什么你只有一个单选按钮 - 它不能取消选择。为什么不使用另一个复选框?
<input type="radio" name="delivery" value="ticket" data-price="10">
英文:
Just have one event handler and one loop
If you only have data-price on the elements you need to loop, then you can change
[...document.querySelectorAll('input[data-price][type=checkbox], input[data-price][type=radio]')].forEach(function(box) {
to
[...document.querySelectorAll('input[data-price]')].forEach(function(box) {
I just wonder why you have a only a single radio - it cannot be un-selected. Why not another checkbox?
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const form = document.getElementById('bookingForm');
const total = document.getElementById('total');
form.addEventListener("click", function(e) {
if (e.target.name === "event[]" || e.target.name === "delivery") {
let totalprice = 0;
[...document.querySelectorAll('input[data-price][type=checkbox], input[data-price][type=radio]')].forEach(function(box) {
if (box.checked) {
totalprice += +box.dataset.price;
}
})
document.querySelector("[name=total]").value = totalprice.toFixed(2);
}
})
<!-- language: lang-html -->
<form id="bookingForm" action="javascript:alert('form submitted');" method="get">
<section id="bookEvents">
<h2>Select Events</h2>
<div class='item'>
<span class='eventTitle'>Event number 1</span>
<span class='eventPrice'>10.50</span>
<span class='chosen'><input type='checkbox' name='event[]' value='1' data-price='10.50'></span>
</div>
<div class='item'>
<span class='eventTitle'>Event number 2</span>
<span class='eventPrice'>5.00</span>
<span class='chosen'><input type='checkbox' name='event[]' value='2' data-price='5.00'></span>
</div>
<section id="Cost">
<input type="radio" name="delivery" value="ticket" data-price="10">
<h2>Total Price</h2>
Total Price <input type="text" name="total" size="12">
</section>
<p><input type="submit" name="submit" value="Book"></p>
</section>
</form>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论