-1
I have some doubts in Javascript where I need to add conditional when selecting a Checkbox, I don’t even know where to start.
Valos there:
4 checkbox, each with a value (10$, 20$, 30$, 40$).
Conditional are:
If you choose the option of $20 and $40, it will give the value of $60, raise the total value by 5$.
Simplifying that is it.
var total = 0;
function opc01(campo) {
  if (campo.checked)
    total += eval(campo.value);
  else
    total -= eval(campo.value);
  document.sistema.total.value = total;
}
var total = 0;
function opc02(campo) {
  if (campo.checked)
    total += eval(campo.value);
  else
    total -= eval(campo.value);
  document.sistema.total.value = total;
}
var total = 0;
function opc03(campo) {
  if (campo.checked)
    total += eval(campo.value);
  else
    total -= eval(campo.value);
  document.sistema.total.value = total;
}
var total = 0;
function opc04(campo) {
  if (campo.checked)
    total += eval(campo.value);
  else
    total -= eval(campo.value);
  document.sistema.total.value = total;
}<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <FORM METHOD=POST ACTION="" name="sistema">
    Opção 01<input type="checkbox" name="opc1" value="10" onClick="opc01(this)"></br>
    Opção 02<input type="checkbox" name="opc2" value="20" onClick="opc02(this)"></br>
    Opção 03<input type="checkbox" name="opc3" value="30" onClick="opc03(this)"></br>
    Opção 04<input type="checkbox" name="opc4" value="40" onClick="opc04(this)"><br>
    <input type="text" name="total" value=""></br>
    <input type="text" name="final" value=""></br>
  </FORM>
  <script type="text/javascript" src="script.js"></script>
</body>
</html>`
Avoid using
eval()your use is discouraged if you want to convert integer strings useparseInt()if you want to convert string to floating point useparseFloat().– Augusto Vasques
As for the question: ...If you choose the option of $20 and $40, you will give the value of $60, subtract the total value by 5$.... and if other checkboxes are selected together the behavior is the same?
– Augusto Vasques
@Augustovasques The intention is to be variable... if user choose the option of 20$ and 40$ decrease 10$, if he choose the option 20$ and 30$, decrease 15$... if he choose three options, decrease 50$....
– Henrique Xavier