Сообщение от CryNet
|
В вашей же реализации такое не прокатывает.
|
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<script>
document.addEventListener('DOMContentLoaded', function() {
var menu = document.getElementsByClassName('menu');
for (var i = 0; i < menu.length; i++) {
menu[i].addEventListener('change', getResult)
}
function getResult(){
var cost = 0;
var kkal = 0;
for (let index = 0; index < menu.length; index++) {
if(menu[index].checked) {
cost += parseFloat(menu[index].getAttribute('data-cost'));
kkal += parseFloat(menu[index].getAttribute('data-kkal'));
}
}
document.getElementById('cost').innerHTML = cost;
document.getElementById('kkal').innerHTML = kkal;
}
getResult();
});
</script>
</head>
<body>
<div id="cost"></div>
<div id="kkal"></div>
<p><input type="checkbox" checked disabled data-cost="10" data-kkal="75" class="menu">Булочка</p>
<p><input type="checkbox" checked disabled data-cost="10" data-kkal="75" class="menu">Булочка</p>
<p><input type="checkbox" checked disabled data-cost="10" data-kkal="75" class="menu">Булочка</p>
</body>
</html>