我将进行快速代码转储,并在提取它时进行解释。
function findTotal() {
var items = new Array();
var itemCount = document.getElementsByClassName("items");
var total = 0;
var id = '';
for (var i = 0; i < itemCount.length; i++) {
id = "c" + (i + 1);
if (document.getElementById(id).checked) {
var element = document.getElementById(id);
total = total + parseInt(element.value);
element.classList.toggle('active');
}
}
console.log(total);
document.getElementById('displayTotal').value = total;
}
所以上面的代码基本上是一长串复选框,并检查哪些复选框被选中,如果选中,则将它们的值相加,然后在底部显示它们的总数。
我今天要重点讲的就是这一部分
if (document.getElementById(id).checked) {
var element = document.getElementById(id);
total = total + parseInt(element.value);
element.classList.toggle('active');
}
我正在寻找它做的是获取已检查的输入的标签(测试每个输入)并切换事件类。
HTML
<label class="hover topping" for="c4"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c4">BABYBEL</label>
<label class="hover topping" for="c5"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c5">TOMATOES</label>
<label class="hover topping" for="c6"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c6">OLIVES</label>
<label class="hover topping" for="c7"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c7">MUSHROOMS</label>
<label class="hover topping" for="c8"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c8">CHICKEN</label>
<label class="hover topping" for="c9"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c9">MOZZARELLA</label>
<label class="hover topping" for="c10"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c10">SALAMI</label>
<label class="hover topping" for="c11"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c11">ONIONS</label>
<label class="hover topping" for="c12"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c12">PEPPERONI</label>
<label class="hover topping" for="c13"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c13">STUFFED CRUST</label>
<label class="hover topping" for="c14"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c14">MEATBALLS</label>
<label class="hover topping" for="c15"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c15">BACON</label>
<label class="hover topping" for="c16"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c16">HAM</label>
<label class="hover topping" for="c17"><input class="items" onclick="findTotal()" type="checkbox" name="topping" value="1.00" id="c17">SHRIMPS</label>
由于 Shalini 请求,我添加了与 javascript 相关的 HTML 部分,不用担心开头的其他 c id。
更新
我现在正在使用 pkmiec 下面提供的代码,但现在我遇到一个问题,当我单击不同的复选框时,我检查的最后一个输入也会被切换。很难解释所以我有 temporarily uploaded它(很抱歉,如果有人再次找到这篇文章,它就消失了)
最佳答案
认识 document.querySelectorAll并使其更短
function findTotal() {
var checkedItems = document.querySelectorAll(".items:checked");
var total = checkedItems.length;
for (var i = 0; i < total; i++) {
//toggle class on input
checkedItems[i].classList.toggle('active');
//toggle class on label
checkedItems[i].parentNode.classList.toggle('active');
}
console.log(total);
document.getElementById('displayTotal').value = total;
}
关于javascript - 检查输入时切换标签类 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36875094/