Ir ao conteúdo
  • Cadastre-se

Checkbox e radiobutton que soma valores


Posts recomendados

Galera eu tenho um codigo aqui para somar as checkbox e exibir o valor total depois 

<?php ini_set('display_errors', true);error_reporting(E_ALL) ;?><script LANGUAGE="JavaScript"><!--function checkChoice(whichbox) {with (whichbox.form) {if (whichbox.checked == false)hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value);elsehiddentotal.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 checkdsfunction checkChoice(whichbox) {with (whichbox.form) {if (whichbox.checked == false)hiddentotal.value = eval(hiddentotal.value) - eval(whichbox.value);elsehiddentotal.value = eval(hiddentotal.value) + eval(whichbox.value);return(formatCurrency(hiddentotal.value));}}// --></script>
<form name=myform class="noformat">                <div class="col-md-6 service-box service-icon-left-more">                  <div class="service-content">                    <h4><center>Tamanho</center></h4>                    <center>                     <ul>                         <div class="btn-group" data-toggle="buttons">                          <?php if($lndet['tamanho'] == 0){ ?>                          <label class="btn btn-default">                            <input type="radio" name="options" id="option1" autocomplete="on"> TAMANHO UNICO                         </label>                        <?php }elseif($lndet['tamanho'] == 1){ ?>                        <label class="btn btn-default">                            <input type="radio" name="options" id="option1" value="9.25" autocomplete="on" checked> 250GR                        </label>                        <label class="btn btn-default">                            <input type="radio" name="options" id="option2" value="11.25" autocomplete="off" onselect="this.form.total.value=checkChoice(this);"> 400GR                        </label>  <label class="btn btn-default">                            <input type="radio" name="options" id="option3" value="13.25" autocomplete="off" onselect="this.form.total.value=checkChoice(this);"> 600GR                        </label>                        <?php }elseif($lndet['tamanho'] == 2){ ?>                        <label class="btn btn-default active">                            <input type="radio" name="options" id="option1" autocomplete="off"> 150GR                        </label>                        <label class="btn btn-default">                            <input type="radio" name="options" id="option2" autocomplete="off"> 300GR                        <?php } ?>                        </div></ul>                    </center>                  </div>                </div>                <!-- End Service Icon 1 -->                 <!-- Start Service Icon 2 -->                <div class="col-md-6 service-box service-icon-left-more">                   <div class="service-content">                    <h4><center>Açai será para?</center></h4>                     <center>                     <ul>                               <div class="btn-group" data-toggle="buttons">  <label class="btn btn-default">    <input type="radio" name="options" id="option1" autocomplete="off" > Buscar  </label>  <label class="btn btn-default">    <input type="radio" name="options" id="option2" autocomplete="off"> Entrega  </label></div></ul></center>                  </div>                </div>                <!-- End Service Icon 2 -->                 <!-- Start Service Icon 3 -->                <center>                <div class="col-md-12 service-box service-icon-left-more">                <table width="650" border="0">                  <tr>                    <td><table width="300" border="0"> <tr>    <td colspan="3"><h4><center>Adicionais</center></h4></td>  </tr>  <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>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="2.5" onClick="this.form.total.value=checkChoice(this);">  Chocolate</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="2" onClick="this.form.total.value=checkChoice(this);">  Kit Kat</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="2" onClick="this.form.total.value=checkChoice(this);">  Confete</td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Morango</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Banana</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Granola</td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Kiwi</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Manga</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Pessego</td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="2.00" onClick="this.form.total.value=checkChoice(this);">  Ovomaltine</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.00" onClick="this.form.total.value=checkChoice(this);">  Mel</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.00" onClick="this.form.total.value=checkChoice(this);">  Paçoca</td>  </tr>    <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  C. Caramelo</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Bis</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);">  Chocoball</td>  </tr></table></td>                    <td><table width="300" border="0"> <tr>    <td colspan="3"><h4><center>Polpa De Fruta</center></h4>    <p><center>Caso queira bater seu açai com uma polpa</center></p></td>  </tr>  <tr>    <td colspan="3"></td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Morango</td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Banana<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Cupuacu<br></td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Tamarindo<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Abacaxi<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Maracuja<br></td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Kiwi<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Manga<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Pessego<br></td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Hortelã<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Uva<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Acerola<br></td>  </tr>  <tr>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Cacau<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Laranja<br></td>    <td><input type="checkbox" autocomplete="off" name="valor" value="1.50" onClick="this.form.total.value=checkChoice(this);"> Limão<br></td>  </tr></table></td>                  </tr></table>                </div>                </center>                <div class="col-md-6 service-box service-icon-left-more">                                                                <br>                                            </div>                <!-- End Service Icon 3 -->                 <!-- Start Service Icon 4 -->                <div class="col-md-6 service-box service-icon-left-more">                  <div class="service-content">                     <br>    <center>  </center>                   </div>                </div>                <!-- End Service Icon 4 -->                 <!-- Start Service Icon 5 -->                <div class="col-md-6 service-box service-icon-left-more">                  <div class="service-content">                    <h4><center>Forma De Pagamento</center></h4>                    <center>                     <ul>                               <div class="btn-group" data-toggle="buttons">  <label class="btn btn-default">    <input type="radio" name="options" id="option1" autocomplete="off"> Cartão De Credito </label>  <label class="btn btn-default">    <input type="radio" name="options" id="option2" autocomplete="off"> Cartão De Debito </label></div></ul></center>                  </div>                </div>                <!-- End Service Icon 5 -->                 <!-- Start Service Icon 6 -->                <div class="col-md-6 service-box service-icon-left-more"><br /> <div class="service-content"><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>  
<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 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>lt;/span></div>     </form>

só que eu tenho uns radio buttons ai que eu preciso atribuir valores a eles e somar só que quando eu uso a mesma ação da checkbox que é 
 
onClick="this.form.total.value=checkChoice(this);" ele soma só que quando desseleciono ele não subtrai ele continua com o valor
 
 
EXEMPLO eu tenho 3 tamanhos de produto
 
250GR, 400GR, 600GR
 
o valor de cada um é
 
250GR = 9,25
400GR = 11,25
600GR = 14,25
 
ai eu seleciono o 250GR no radio button ele pega e soma 9,25 ao valor total, mas ai quando eu clico em 400GR ele não subtrai os 9,25 do valor total, ele só soma os 11,25 
 
alguem sabe como eu conserto isso? kkkk

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

 

GRÁTIS: ebook Redes Wi-Fi – 2ª Edição

EBOOK GRÁTIS!

CLIQUE AQUI E BAIXE AGORA MESMO!