我正在尝试显示已检查的所有复选框的总值我只尝试过此解决方案
我的描述非常精确且不言自明
Javascript功能
如何显示已选中的所有复选框的总和。尝试通过计算所有选中的复选框来显示总价。
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>On the go</title>
<link rel="stylesheet" type="text/css" href="menu.scss">
<script>
function calcAndShowTotal() { **
* // declaring the name and total***
var hotbeverage = document.querySelectorAll('input[name="hotbeverage"]');
var total = 0; **
* // checking if any checkboxes have been selected***
for (i = 0; i < hotbeverage.length; i++) { **
* // if the checkbox is selected add the value to total***
if (hotbeverage[i].checked) {
total = total + hotbeverage[i].value;
}
document.getElementById("amount").value = "You order total is R" + total;
}
</script>
***// adding checkboxes***
</head>
<body>
<h1>Hot Beverages</h1>
<div id="pricelist">
<input type="checkbox" name="hotbeverage" value="item1" price="25.00">Americano <b>R25.00</b><br>
<input type="checkbox" name="hotbeverage" value="item2" price="2">Caffe Latte <b>R30.00</b><br>
<input type="checkbox" name="hotbeverage" value="item3" price="3">Cappuccino <b>R15.00</b><br>
<input type="checkbox" name="hotbeverage" value="item4" price="4">Hot Chocolate<b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item5" price="5">Chai Latte <b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item6" price="6">ButterScotch latte<b>R28.00</b><br>
<input type="text" id="amount" value="0" />
</div>
</body>
</html>
我希望文本字段填充值
当实际值在price
属性中时,当您尝试计算输入值时,会出现主要问题,因此您可以通过.value
更改.getAttribute('price')
。
注意:当你需要一个自定义属性时,使用data-*
属性总是更好,所以data-price
会更高效,然后你可以获得如下值:
parseFloat( hotbeverage[i].dataset.price );
document.querySelector('#calc').addEventListener('click', calcAndShowTotal);
function calcAndShowTotal() {
var hotbeverage = document.querySelectorAll('input[name="hotbeverage"]');
var total = 0;
for (i = 0; i < hotbeverage.length; i++) {
if (hotbeverage[i].checked) {
total += parseFloat( hotbeverage[i].getAttribute('price') );
}
}
document.getElementById("amount").value = "You order total is R " + total;
}
<h1>Hot Beverages</h1>
<div id="pricelist">
<input type="checkbox" name="hotbeverage" value="item1" price="25.00">Americano <b>R25.00</b><br>
<input type="checkbox" name="hotbeverage" value="item2" price="2">Caffe Latte <b>R30.00</b><br>
<input type="checkbox" name="hotbeverage" value="item3" price="3">Cappuccino <b>R15.00</b><br>
<input type="checkbox" name="hotbeverage" value="item4" price="4">Hot Chocolate<b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item5" price="5">Chai Latte <b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item6" price="6">ButterScotch latte<b>R28.00</b><br>
<input type="text" id="amount" value="0" />
</div>
<button id="calc">Calculate</button>
要显示所有选中的复选框的总数,并通过计算所有选中的复选框显示总价,您可以使用以下代码:html标记:
测试1 测试2 测试3 测试4
javaScript代码:
jQuery(document).ready(function($) {
var sum = 0;
$('#pakker :checkbox').click(function() {
sum = 0;
$('#pakker :checkbox:checked').each(function(idx, elm) {
sum += parseInt(elm.value, 10);
});
$('#sum').html(sum);
});
});
有关更多详细信息,请参阅以下链接。 http://jsfiddle.net/vaKWs/6/
以下是代码中的错误:
calcAndShowTotal
。您应该将事件附加到所有复选框以查看更改。price
。使用getAttribute
来复选框的价格。i
。在它之前使用let。let hotbeverage = document.querySelectorAll('input[name="hotbeverage"]');
hotbeverage.forEach(x => {
x.addEventListener('change',calcAndShowTotal)
})
function calcAndShowTotal() {
// declaring the name and total***
var total = 0;
// checking if any checkboxes have been selected***
for (let i = 0; i < hotbeverage.length; i++) {
// if the checkbox is selected add the value to total***
if (hotbeverage[i].checked) {
total = total + (+hotbeverage[i].getAttribute('price'))
}
document.getElementById("amount").value = "You order total is R" + total;
}
}
<h1>Hot Beverages</h1>
<div id="pricelist">
<input type="checkbox" name="hotbeverage" value="item1" price="25.00">Americano <b>R25.00</b><br>
<input type="checkbox" name="hotbeverage" value="item2" price="2">Caffe Latte <b>R30.00</b><br>
<input type="checkbox" name="hotbeverage" value="item3" price="3">Cappuccino <b>R15.00</b><br>
<input type="checkbox" name="hotbeverage" value="item4" price="4">Hot Chocolate<b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item5" price="5">Chai Latte <b>R20.00</b><br>
<input type="checkbox" name="hotbeverage" value="item6" price="6">ButterScotch latte<b>R28.00</b><br>
<input type="text" id="amount" value="0" />
</div>