Telles85 Posted April 29, 2022 at 04:15 PM Report Share #625864 Posted April 29, 2022 at 04:15 PM Queria somar os valores de várias checkbox, cada vez que fizer check numa checkbox ele acrescenta o valor, quando retirar o check ele retira o valor. Sou iniciante e estou com dúvidas neste ponto, por favor. Deixo aqui o meu formulário HTML: <form action="" name="formulario" onchange="calcularValores()"> <input type="checkbox" id="tipo-pagina" name="separadores" value="400">1 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">2 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">3 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">4 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">5 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">6 <input type="checkbox" id="tipo-pagina" name="separadores" value="400">7 <input type="text" id="valororcamento" name="valororcamento" value=""> </form> Link to comment Share on other sites More sharing options...
Solution Zex Posted April 29, 2022 at 09:23 PM Solution Report Share #625865 Posted April 29, 2022 at 09:23 PM <script> var soma1 = 0 var soma2 = 0 function somar(check) { if ( check.checked ) soma1 += 1*check.value; else soma1 -= 1*check.value; document.getElementById("valororcamento1").value = soma1 } function calcularValores() { soma2 = 0 for (var i=1; i<=7; i++) { var check = document.getElementById("check"+i) if ( check.checked ) soma2 += 1*check.value; } document.getElementById("valororcamento2").value = soma2 } </script> <body> <form action="" name="formulario" onchange="calcularValores()"> <input type="checkbox" id="check1" value="400" onchange="somar(this)">1</input> <input type="checkbox" id="check2" value="400" onchange="somar(this)">2</input> <input type="checkbox" id="check3" value="400" onchange="somar(this)">3</input> <input type="checkbox" id="check4" value="400" onchange="somar(this)">4</input> <input type="checkbox" id="check5" value="400" onchange="somar(this)">5</input> <input type="checkbox" id="check6" value="400" onchange="somar(this)">6</input> <input type="checkbox" id="check7" value="400" onchange="somar(this)">7</input> <input type="text" id="valororcamento1" value=""> <input type="text" id="valororcamento2" value=""> </form> Citação Link to comment Share on other sites More sharing options...
Telles85 Posted April 30, 2022 at 03:02 PM Author Report Share #625870 Posted April 30, 2022 at 03:02 PM Fantástico, muito obrigado, é mesmo isso que precisava. Para quem é iniciante não é fácil. Obrigadíssimo. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now