Version compl�te: jsB@nk » Form » Checkbox » Simple JavaScript pour Auto-Somme des cases ? cocher
URL: https://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html
Cet exemple de code JavaScript utilise une boucle for pour calculer la somme de valeurs case JavaScript .
Version compl�te: jsB@nk » Form » Checkbox » Simple JavaScript pour Auto-Somme des cases ? cocher
URL: https://www.javascriptbank.com/simple-javascript-auto-sum-with-checkboxes.html
<script type="text/javascript">// Created by: Jay Rumsey | http://www.nova.edu/~rumsey/// This script downloaded from JavaScriptBank.comfunction UpdateCost() { var sum = 0; var gn, elem; for (i=0; i<5; i++) { gn = 'game'+i; elem = document.getElementById(gn); if (elem.checked == true) { sum += Number(elem.value); } } document.getElementById('totalcost').value = sum.toFixed(2);} </script>
<input type="checkbox" id='game0' value="9.99" onclick="UpdateCost()">Game 1 ( 9.99)<br><input type="checkbox" id='game1' value="19.99" onclick="UpdateCost()">Game 2 (19.99)<br><input type="checkbox" id='game2' value="27.50" onclick="UpdateCost()">Game 3 (27.50)<br><input type="checkbox" id='game3' value="45.65" onclick="UpdateCost()">Game 4 (45.65)<br><input type="checkbox" id='game4' value="87.20" onclick="UpdateCost()">Game 5 (87.20)<br>