1
Guys I 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 sums the checkbox selected and displays the total value also in the same input my problem is being the following, at the time to select the radiobutton he adds the selected radiobutton but when I mark another radiobutton he just doesn’t subtract
EXAMPLE: RADIO1 = 10 REAIS RADIO2 = 15 REAIS RADIO3 = 40 REAIS
if I select the RADIO1 it adds 10 real, but if by chance I change from RADIO1 to RADIO3 it does not subtract the RADIO1 it only adds the RADIO3 then the total value is 50 instead of 40...
Below is the code..
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 it so I complete this system.
I don’t know what happened but the code here on the site it works but when I put it on some online page, it doesn’t display the result.
If you want to take a look here access here: http://acaiamarena.com.br/modulos/teste.php
tips: don’t use the function
eval()
, not a good practice; use all keys even for single line instructions for easy reading– Pedro Sanção