Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Tayso

Selecionar todos e somar todos checkbox

Recommended Posts

Boa Tarde Pessoal, sou novo no forun, e to com um problema, tenho uma soma de checkbox, e um selecionar todos os checkbox, porem quando eu seleciono um por um a soma se realiza, quando eu clicko em selecionar todos ele seleciona mas naum soma, vou postar o codigo abaixo.

<html><body><head><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 ("R$" + 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></head><body><center><table><tr><td><form name=myform><td class="tleft" align="center"><a href="java script:checkall('myform','somas',true)">Check All</a><br><a href="java script:checkall('myform','somas',false)">Uncheck All</a></td><pre><font face=verdana size=1>COMPRASDISCO   R$ 10,00	 <input type=checkbox name=Steak value="10.00" onClick="this.form.total.value=checkChoice(this);">CD  R$ 20,00			<input type=checkbox name=Chicken value="20.00" onClick="this.form.total.value=checkChoice(this);">TECLADO	R$ 30,00  <input type=checkbox name=Sushi value="30.00" onClick="this.form.total.value=checkChoice(this);"><br>Total: <input type="text" name="total" value="" size=6 readonly><input type=hidden name=hiddentotal value=0></pre></form></td></tr></table></center></body></html>
Algurm pode me ajudar, ja tentei tudo que estava ao meu nivel, mas naum manjo muito de javascript, desde de ja muito obeigado

Compartilhar este post


Link para o post
Compartilhar em outros sites

ta ai a funcao para somar tudo e zerar

 

<script type="text/javascript">

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 ("R$" + num + "." + cents);

}

function sumAll(){

var elements = document.myform.elements;

var total=0;

for(var i=0,e; i<elements.length,e=elements; i++){

if(e.type.toLowerCase()=='checkbox'){

total+=parseFloat(e.value);

e.checked=true

}

}

document.myform['total'].value = formatCurrency(total);

}

function zero(){

var elements = document.myform.elements;

for(var i=0,e; i<elements.length,e=elements; i++){

if(e.type.toLowerCase()=='checkbox')

e.checked=false

}

document.myform['total'].value = 'R$0,00';

}

</script>

 

<form name=myform>

<input type="button" value="selecionar" onClick="sumAll()" >

<input type="button" value="deselecionar" onClick="zero()" >

<br>

<input type='checkbox' name=Steak value="10.00" >

DISCO R$ 10,00 <br>

<input type='checkbox' name=Chicken value="20.00" >

CD R$ 20,00 <br>

<input type='checkbox' name=Sushi value="30.00" >

TECLADO R$ 30,00 <br>

Total:

<input type="text" name="total" value="" size=6 readonly>

 

</form>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.