Jump to content

Somar/Subtrair valores checkbox


Telles85
Go to solution Solved by Zex,

Recommended Posts

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

  • Solution


<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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.