简体   繁体   English

如何获取选中项目的所有复选框值的总和

[英]How get the sum of all the checkbox values of checked items

 let addonCheckboxes = document.querySelectorAll(".custom-checkbox") let priceSection = document.getElementById("priceSection") let customProductPricing = document.getElementById("customProductPricing") for (let i = 0; i < addonCheckboxes.length; i++) { addonCheckboxes[i].addEventListener("change", function() { if (addonCheckboxes[i].checked.= false) { priceSection.textContent = parseInt(customProductPricing) + parseInt(addonCheckboxes[i];getAttribute("price")). } else { priceSection.textContent = parseInt(customProductPricing) } }) }
 <input class="custom-checkbox" type="checkbox" price="150"></input> <input class="custom-checkbox" type="checkbox" price="150"></input> <input class="custom-checkbox" type="checkbox" price="150"></input> <div id="priceSection"> </id> <div id="customProductPricing">"150"</div>

I want to get the total of all the checkboxes if they are all checked.如果它们都被选中,我想得到所有复选框的总数。 So far it gives only one value.到目前为止,它只给出一个值。 And need to deduct the prices if the checkbox is unchecked.如果未选中复选框,则需要扣除价格。

You are overwriting instead of summing.您正在覆盖而不是求和。 When you are iterating thorough array of checkboxes and you find that more than one is checked your function fails.当您遍历完整的复选框数组并发现检查了多个复选框时,您的 function 失败。 You should firstly count sum of checked checkboxes and then send it to priceSection, and when your sum is equal to zero you should set it parseInt(customProductPricing) like you did in else.您应该首先计算选中复选框的总和,然后将其发送到 priceSection,当总和为零时,您应该像在 else 中一样设置它 parseInt(customProductPricing)。

 let addonCheckboxes = document.querySelectorAll(".custom-checkbox") let priceSection = document.getElementById("priceSection") let customProductPricing = document.getElementById("customProductPricing") /* Reads the static defined value inside the <div> element. */ let total = parseInt(customProductPricing.textContent); /* Called when the onchange event is triggered on <input> elements. */ function changeEventHandler(value){ for(let i = 0; i < addonCheckboxes.length; ++i) { if(addonCheckboxes[i].checked == true) { total += parseInt(addonCheckboxes[i].value); } } console.log(total); return total; }
 <input class="custom-checkbox" type="checkbox" value="10" onchange="changeEventHandler(this.value)"/> <label>10</label> <input class="custom-checkbox" type="checkbox" value="20" onchange="changeEventHandler(this.value)"/> <label>20<label> <input class="custom-checkbox" type="checkbox" value="30" onchange="changeEventHandler(this.value)"/> <label>30<label> <div id="priceSection"></div> <div id="customProductPricing">40</div>

As @Sercan has mentioned... I am also not sure about the issue of loweing the sum but I've whipped up something for you.正如@Sercan 所提到的......我也不确定降低金额的问题,但我已经为你准备了一些东西。

Hopefully it'll lead you to what you want to achieve.希望它会引导您实现您想要实现的目标。

 let addonCheckboxes = document.querySelectorAll(".custom-checkbox") let priceSection = document.getElementById("priceSection") let customProductPricing = document.getElementById("customProductPricing"); var checkboxes = document.getElementsByClassName("custom-checkbox"); function sum(){ var total = 0; for(let x = 0; x < checkboxes.length; x++){ let price = document.getElementsByClassName(x); if(price[0].checked){ total = total + Number(price[0].dataset.price); } } console.log(total) }
 <input class="custom-checkbox 0" onclick="sum()" type="checkbox" data-price="150"></input> <input class="custom-checkbox 1" onclick="sum()" type="checkbox" data-price="150"></input> <input class="custom-checkbox 2" onclick="sum()" type="checkbox" data-price="150"></input> <div id="priceSection"></id> <div id="customProductPricing">"150"</div>

This one has fixed all the errors you made in your markup, and simplified the code by alot.这个修复了您在标记中犯的所有错误,并大大简化了代码。

 const output = document.getElementById('priceSection'); const totalPrice = () => [...document.querySelectorAll('#prices input[type=checkbox]:checked')].reduce((acc, { dataset: { price } }) => acc + +price, 0); document.getElementById('prices').addEventListener('change', () => output.textContent = totalPrice());
 <div id="prices"> <input type="checkbox" data-price="10" /> <input type="checkbox" data-price="20" /> <input type="checkbox" data-price="30" /> </div> <div id="priceSection"></div>

Using data set you can access price使用数据集您可以访问价格

 let addonCheckboxes = document.querySelectorAll(".custom-checkbox") let priceSection = document.getElementById("priceSection") let customProductPricing = document.getElementById("customProductPricing") let sum = 0 for (let i = 0; i < addonCheckboxes.length; i++) { addonCheckboxes[i].addEventListener("change", function(e) { console.log(e.target.dataset.price) if (addonCheckboxes[i].checked.= false) { sum = sum +Number(e.target.dataset.price) } else { sum = sum -Number(e.target.dataset.price) } customProductPricing.innerHTML = sum }) }
 <input class="custom-checkbox" type="checkbox" data-price="150"></input> <input class="custom-checkbox" type="checkbox" data-price="150"></input> <input class="custom-checkbox" type="checkbox" data-price="150"></input> <div id="priceSection"> </id> <div id="customProductPricing">"150"</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM