Sammentælling af totaler - automatisk
Hej,jeg har fået sammensat mig en kode, som måske kan virke lidt "tung", men den virker, næsten til mit formål :-)
Jeg har en side med 11 produkter - ved indtastning i felterne skal den regne prisen ud efter hånden.
Jeg har fået den til at regne prisen for det enkelte produkt - og har med onblur() fået den til at opdatere i alt feltet for den pågældende linie, når den "forlades".
Jeg skal dog bruge en sammentællng til sidst - som giver mig en samlet total - er der nogen, der vil hjælpe mig med denne del?
Det jeg skal er at have lagt mine:
total1, total2, total3, total4, total5, total6, total7, total8, total9, total10 og total11 - sammen - total feltet skal tælle op efterhånden...
Min kode er som følger:
<script type="text/javascript">function total1(){var antal1 = document.getElementById('ANTAL1').value; var pris1 = document.getElementById('PRIS1').value; var total1;if(pris1 >0 && antal1 > 0) total1 = antal1*pris1;else total1 = 0; document.getElementById('IALT1').value = total1; }
function total2(){var antal2 = document.getElementById('ANTAL2').value; var pris2 = document.getElementById('PRIS2').value; var total;if(pris2 >0 && antal2 > 0) total2 = antal2*pris2;else total2 = 0; document.getElementById('IALT2').value = total2; }
function total3(){var antal3 = document.getElementById('ANTAL3').value; var pris3 = document.getElementById('PRIS3').value; var total3;if(pris3 >0 && antal3 > 0) total3 = antal3*pris3;else total3 = 0; document.getElementById('IALT3').value = total3; }
function total4(){var antal4 = document.getElementById('ANTAL4').value; var pris4 = document.getElementById('PRIS4').value; var tota4l;if(pris4 >0 && antal4 > 0) total4 = antal4*pris4;else total4 = 0; document.getElementById('IALT4').value = total4; }
function total5(){var antal5 = document.getElementById('ANTAL5').value; var pris5 = document.getElementById('PRIS5').value; var total5;if(pris5 >0 && antal5 > 0) total5 = antal5*pris5;else total5 = 0; document.getElementById('IALT5').value = total5; }
function total6(){var antal6 = document.getElementById('ANTAL6').value; var pris6 = document.getElementById('PRIS6').value; var total6;if(pris6 >0 && antal6 > 0) total6 = antal6*pris6;else total6 = 0; document.getElementById('IALT6').value = total6; }
function total7(){var antal7 = document.getElementById('ANTAL7').value; var pris7 = document.getElementById('PRIS7').value; var total7;if(pris7 >0 && antal7 > 0) total7 = antal7*pris7;else total7 = 0; document.getElementById('IALT7').value = total7; }
function total8(){var antal8 = document.getElementById('ANTAL8').value; var pris8 = document.getElementById('PRIS8').value; var total8;if(pris8 >0 && antal8 > 0) total8 = antal8*pris8;else total8 = 0; document.getElementById('IALT8').value = total8; }
function total9(){var antal9 = document.getElementById('ANTAL9').value; var pris9 = document.getElementById('PRIS9').value; var total9;if(pris9 >0 && antal9 > 0) total9 = antal9*pris9;else total9 = 0; document.getElementById('IALT9').value = total9; }
function total10(){var antal10 = document.getElementById('ANTAL10').value; var pris10 = document.getElementById('PRIS10').value; var total10;if(pris10 >0 && antal10 > 0) total10 = antal10*pris10;else total10 = 0; document.getElementById('IALT10').value = total10; }
function total11(){var antal11 = document.getElementById('ANTAL11').value; var pris11 = document.getElementById('PRIS11').value; var total11;if(pris11 >0 && antal11 > 0) total11 = antal11*pris11;else total11 = 0; document.getElementById('IALT11').value = total11; }
</script>
<form id="test">
<TABLE Border=1>
<TR>
<TD>Tekst:</TD><TD>Pris:</TD><TD>Antal:</TD><TD>I alt:</TD>
</TR>
<TR>
<TD>Pakke 1</TD><TD><input type="text" id="PRIS1" name="PRIS1" size="28" onblur="total1()" value=100></TD><TD><input type="text" id="ANTAL1" name="ANTAL1" size="28" onblur="total1()" onblur="totalsamlet()"></TD><TD><input type="text" id ="IALT1" name="IALT1" size="28"></TD>
</TR>
<TR>
<TD>Pakke 2</TD><TD><input type="text" id="PRIS2" name="PRIS2" size="28" onblur="total2()" value=200></TD><TD><input type="text" id="ANTAL2" name="ANTAL2" size="28" onblur="total2()"></TD><TD><input type="text" id ="IALT2" name="IALT2" size="28"></TD>
</TR>
<TR>
<TD>Pakke 3</TD><TD><input type="text" id="PRIS3" name="PRIS3" size="28" onblur="total3()" value=300></TD><TD><input type="text" id="ANTAL3" name="ANTAL3" size="28" onblur="total3()"></TD><TD><input type="text" id ="IALT3" name="IALT3" size="28"></TD>
</TR>
<TR>
<TD>Pakke 4</TD><TD><input type="text" id="PRIS4" name="PRIS4" size="28" onblur="total4()" value=400></TD><TD><input type="text" id="ANTAL4" name="ANTAL4" size="28" onblur="total4()"></TD><TD><input type="text" id ="IALT4" name="IALT4" size="28"></TD>
</TR>
<TR>
<TD>Pakke 5</TD><TD><input type="text" id="PRIS5" name="PRIS5" size="28" onblur="total5()" value=500></TD><TD><input type="text" id="ANTAL5" name="ANTAL5" size="28" onblur="total5()"></TD><TD><input type="text" id ="IALT5" name="IALT5" size="28"></TD>
</TR>
<TR>
<TD>Pakke 6</TD><TD><input type="text" id="PRIS6" name="PRIS6" size="28" onblur="total6()" value=600></TD><TD><input type="text" id="ANTAL6" name="ANTAL6" size="28" onblur="total6()"></TD><TD><input type="text" id ="IALT6" name="IALT6" size="28"></TD>
</TR>
<TR>
<TD>Pakke 7</TD><TD><input type="text" id="PRIS7" name="PRIS7" size="28" onblur="total7()" value=700></TD><TD><input type="text" id="ANTAL7" name="ANTAL7" size="28" onblur="total7()"></TD><TD><input type="text" id ="IALT7" name="IALT7" size="28"></TD>
</TR>
<TR>
<TD>Pakke 8</TD><TD><input type="text" id="PRIS8" name="PRIS8" size="28" onblur="total8()" value=800></TD><TD><input type="text" id="ANTAL8" name="ANTAL8" size="28" onblur="total8()"></TD><TD><input type="text" id ="IALT8" name="IALT8" size="28"></TD>
</TR>
<TR>
<TD>Pakke 9</TD><TD><input type="text" id="PRIS9" name="PRIS9" size="28" onblur="total9()" value=900></TD><TD><input type="text" id="ANTAL9" name="ANTAL9" size="28" onblur="total9()"></TD><TD><input type="text" id ="IALT9" name="IALT9" size="28"></TD>
</TR>
<TR>
<TD>Pakke 10</TD><TD><input type="text" id="PRIS10" name="PRIS10" size="28" onblur="total10()" value=1000></TD><TD><input type="text" id="ANTAL10" name="ANTAL10" size="28" onblur="total10()"></TD><TD><input type="text" id ="IALT10" name="IALT10" size="28"></TD>
</TR>
<TR>
<TD>Pakke 11</TD><TD><input type="text" id="PRIS11" name="PRIS11" size="28" onblur="total11()" value=1100></TD><TD><input type="text" id="ANTAL11" name="ANTAL11" size="28" onblur="total11()"></TD><TD><input type="text" id ="IALT11" name="IALT11" size="28"></TD>
</TR>
<TR>
<TD> </TD><TD> </TD><TD> </TD><TD> </TD>
</TR>
<TR>
<TD>Samlet pris:</TD><TD><center>-</center></TD><TD><center>-</center></TD><TD><input type="text" id ="totalsamlet" name="totalsamlet" size="28"></TD>
</TR>
</TABLE>