I am here a code in js that when selecting a checkbox it displays the value of the checkbox in an input and if I select more than one it adds the selected checkboxes and displays the total value also in the same input My problem is this, the time you select the radiobutton it adds the selected radiobutton but when I dial another radiobutton it just does not subtract
EXAMPLE: RADIO1 = 10 REAIS RADIO2 = 15 REAIS RADIO3 = 40 REAIS
If I select RADIO1 it adds 10 reais, but if by chance I change from RADIO1 to RADIO3 it does not subtract RADIO1 it only adds RADIO3 to it the total value is 50 instead of 40 ...
Follow the code below.
function checkChoice(whichbox) {
with(whichbox.form) {
if (whichbox.checked == false)
hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value);
else
hiddentotal.value = eval(hiddentotal.value) + eval(whichbox.value);
return (formatCurrency(hiddentotal.value));
}
}
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g, '');
if (isNaN(num)) num = "0";
cents = Math.floor((num * 100 + 0.5) % 100);
num = Math.floor((num * 100 + 0.5) / 100).toString();
if (cents < 10) cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
return ("" + num + "." + cents);
}
// funcoes somas de checkds
function checkChoice(whichbox) {
with(whichbox.form) {
if (whichbox.checked == false)
hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value);
else
hiddentotal.value = eval(hiddentotal.value) + eval(whichbox.value);
return (formatCurrency(hiddentotal.value));
}
}
<form name=myform class="noformat">
<label class="btn btn-default">
<input type="radio" name="tamanho" id="option1" value="9.25" autocomplete="off" onchange="this.form.total.value=checkChoice(this);">250GR
</label>
<label class="btn btn-default">
<input type="radio" name="tamanho" id="option2" value="11.25" autocomplete="off" onchange="this.form.total.value=checkChoice(this);">400GR
</label>
<label class="btn btn-default">
<input type="radio" name="tamanho" id="option3" value="14.25" autocomplete="off" onchange="this.form.total.value=checkChoice(this);">600GR
</label>
<tr>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="2" onClick="this.form.total.value=checkChoice(this);">L. Ninho</td>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="3" onClick="this.form.total.value=checkChoice(this);">Nutella</td>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="3" onClick="this.form.total.value=checkChoice(this);">Chantilly</td>
</tr>
<tr>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="1.5" onClick="this.form.total.value=checkChoice(this);">L. Condensado</td>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="1.5" onClick="this.form.total.value=checkChoice(this);">S. Valsa</td>
<td>
<input type="checkbox" autocomplete="off" name="valor" value="2.5" onClick="this.form.total.value=checkChoice(this);">Sorvete</td>
</tr>
<div class="input-group">
<span class="input-group-addon">Valor Total:</span>
<input class="form-control input-lg" id="disabledInput" name="total" type="text" placeholder="" readonly disabled>
<input type=hidden name=hiddentotal value=0>
<span class="input-group-addon">R$</span>
</div>
</form>
Someone help me miss this so I can complete this system.
I do not know what happened but the code here on the site works but when I put it in some online page, it does not display the result.
If you want to take a look here, you can access here: link