Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [JavaScript][PHP] Błędnie działa funkcja suma().
Forum PHP.pl > Forum > Przedszkole
lekosm2
Witam, mam skrypt w JS zliczający wynik z input, niestety nie dział ona do końca tak jakbym chciał o tuż, gdy nie zaznaczę wszystkich checkboxów, to nie wyświetla mi sumy tylko wartość "NaN".
Poniżej wstawiam skrypt JS, który mam.
  1. <script type="text/javascript">
  2. var tab1 = document.getElementsByName('tq1');
  3. var tab2 = document.getElementsByName('tq2');
  4. var tab3 = document.getElementsByName('tq3');
  5. var tab4 = document.getElementsByName('tq4');
  6. var tab5 = document.getElementsByName('tq5');
  7. var tab6 = document.getElementsByName('tq6');
  8. var tab7 = document.getElementsByName('tq7');
  9. var tab8 = document.getElementsByName('tq8');
  10. var tab9 = document.getElementsByName('tq9');
  11. var tab10 = document.getElementsByName('tq10');
  12. var tab11 = document.getElementsByName('tq11');
  13. var tab12 = document.getElementsByName('tq12');
  14. var tab13 = document.getElementsByName('tq13');
  15. var tab14 = document.getElementsByName('tq14');
  16. var tab15 = document.getElementsByName('tq15');
  17. var tab16 = document.getElementsByName('tq16');
  18. var tab17 = document.getElementsByName('tq17');
  19. var tab18 = document.getElementsByName('tq18');
  20. var tab19 = document.getElementsByName('tq19');
  21. var tab20 = document.getElementsByName('tq20');
  22.  
  23. function suma ()
  24. {
  25. var pole1;
  26. var pole2;
  27. var pole3;
  28. var pole4;
  29. var pole5;
  30. var pole6;
  31. var pole7;
  32. var pole8;
  33. var pole9;
  34. var pole10;
  35. var pole11;
  36. var pole12;
  37. var pole13;
  38. var pole14;
  39. var pole15;
  40. var pole16;
  41. var pole17;
  42. var pole18;
  43. var pole19;
  44. var pole20;
  45.  
  46. for(var i=0;i<tab1.length;i++)
  47. {
  48. if(tab1[i].checked) pole1 = tab1[i].value;
  49. }
  50. for(var i=0;i<tab2.length;i++)
  51. {
  52. if(tab2[i].checked) pole2 = tab2[i].value;
  53. }
  54. for(var i=0;i<tab3.length;i++)
  55. {
  56. if(tab3[i].checked) pole3 = tab3[i].value;
  57. }
  58. for(var i=0;i<tab4.length;i++)
  59. {
  60. if(tab4[i].checked) pole4 = tab4[i].value;
  61. }
  62. for(var i=0;i<tab5.length;i++)
  63. {
  64. if(tab5[i].checked) pole5 = tab5[i].value;
  65. }
  66. for(var i=0;i<tab6.length;i++)
  67. {
  68. if(tab6[i].checked) pole6 = tab6[i].value;
  69. }
  70. for(var i=0;i<tab7.length;i++)
  71. {
  72. if(tab7[i].checked) pole7 = tab7[i].value;
  73. }
  74. for(var i=0;i<tab8.length;i++)
  75. {
  76. if(tab8[i].checked) pole8 = tab8[i].value;
  77. }
  78. for(var i=0;i<tab9.length;i++)
  79. {
  80. if(tab9[i].checked) pole9 = tab9[i].value;
  81. }
  82. for(var i=0;i<tab10.length;i++)
  83. {
  84. if(tab10[i].checked) pole10 = tab10[i].value;
  85. }
  86. for(var i=0;i<tab11.length;i++)
  87. {
  88. if(tab11[i].checked) pole11 = tab11[i].value;
  89. }
  90. for(var i=0;i<tab12.length;i++)
  91. {
  92. if(tab12[i].checked) pole12 = tab12[i].value;
  93. }
  94. for(var i=0;i<tab13.length;i++)
  95. {
  96. if(tab13[i].checked) pole13 = tab13[i].value;
  97. }
  98. for(var i=0;i<tab14.length;i++)
  99. {
  100. if(tab14[i].checked) pole14 = tab14[i].value;
  101. }
  102. for(var i=0;i<tab15.length;i++)
  103. {
  104. if(tab15[i].checked) pole15 = tab15[i].value;
  105. }
  106. for(var i=0;i<tab16.length;i++)
  107. {
  108. if(tab16[i].checked) pole16 = tab16[i].value;
  109. }
  110. for(var i=0;i<tab17.length;i++)
  111. {
  112. if(tab17[i].checked) pole17 = tab17[i].value;
  113. }
  114. for(var i=0;i<tab18.length;i++)
  115. {
  116. if(tab18[i].checked) pole18 = tab18[i].value;
  117. }
  118. for(var i=0;i<tab19.length;i++)
  119. {
  120. if(tab19[i].checked) pole19 = tab19[i].value;
  121. }
  122. for(var i=0;i<tab20.length;i++)
  123. {
  124. if(tab20[i].checked) pole20 = tab20[i].value;
  125. }
  126. var wynik = parseInt(pole1)+parseInt(pole2)+parseInt(pole3)+parseInt(pole4)+parseInt(pole5)+parseInt(pole6)+parseInt(pole7)+parseInt(pole8)+parseInt(pole9)+parseInt(pole10)+parseInt(pole11)+parseInt(pole12)+parseInt(pole13)+parseInt(pole14)+parseInt(pole15)+parseInt(pole16)+parseInt(pole17)+parseInt(pole18)+parseInt(pole19)+parseInt(pole20);
  127. alert('Zebrane punkty:'+ wynik);
  128. return false;
  129. }
  130. document.getElementById('ale').onclick = suma;


Wiecie jak to naprawić by działało tak jak chcę?
Z góry dzięki za pomoc i pozdrawiam. smile.gif
darko
Dzizas, zrób z tego tablice, bo nawet czytać się tego nie chce.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.