Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP] kalkulator - kłopot z deklaracją
Forum PHP.pl > Forum > Przedszkole
-poczatkujacy-
Witam

Potrzebuje prosty kalkulator.
Poszukałem, wyszperałem i mam.
Teraz zmieniłem 1 rzecz i przestał działac.

Kalkulator to tylko taka tableka
Produkt / ilosc / cena / suma
np.
Produkt#1 / 2 / 5 / =10

Poprawny, dzialajacy kod

Kod
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="pl">
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Kalkulator</title>
      <script type="text/javascript" src="http://www.google.com/jsapi"></script>
      <script type="text/javascript">
         google.load("jquery", "1");
      </script>
      <script type="text/javascript">
         function update(){
            var sum = 0;
            $("#calculator > tbody > tr").each(function(){
               var price  = parseFloat($(this).find("td:eq(2)").text());
               var amount = parseFloat($(this).find("td:eq(3)").find("input:first").val());
               var value  = $(this).find("td:eq(4)");
               if (amount > 0) {
                  value.text(price*amount);
                  sum += price*amount;
               } else {
                  value.text(0);
               }
            });
            $("#summary").text(sum);
         }
         $(document).ready(function(){
            update();
            $("#calculator input").keyup(function(){
               update();
            });
         });
      </script>
   </head>
   <body>
      <table id="calculator">
         <thead>
            <tr>
               <th>Rodzaj usługi</th>
               <th>Jm</th>
               <th>Cena</th>
               <th>Ilość</th>
               <th>Wartość</th>
            </tr>
         </thead>
         <tfoot>
            <tr>
               <th colspan="4">Razem</th>
               <th id="summary"></th>
            </tr>
         </tfoot>
         <tbody>
            <tr>
               <td>Produkt 1</td>
               <td>1 szt</td>
               <td>12.00</td>
               <td><input type="text" name="gadzie-gipsowe"></td>
               <td></td>
            </tr>
            <tr>
               <td>Produkt 2</td>
               <td>1 szt.</td>
               <td>5.00</td>
               <td><input type="text" name="malowanie"></td>
               <td></td>
            </tr>
          
         </tbody>
      </table>
   </body>
</html>


Powyzszy dziala, ale ma zadeklarowane ceny.
Chcialby miec 2 kalkulator, gdzie user sam wpisze cene i ilosc.


Zrobilem zmiane w kodzie i tu pojawia sie problem - nie dziala.
Co zle zrobilem?
Sam chcialem, ale juz nie wiem co zle zrobilem.
Pewnie cos prostego :)


Kod
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf 8">
<title>Kalkulator</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1");
</script>
<script type="text/javascript">
function update(){
var sum = 0;
$("#calculator > tbody > tr").each(function(){
var price = parseFloat($(this).find("td:eq(2)").find("input:first").val());
var amount = parseFloat($(this).find("td:eq(3)").find("input:second").val());
var value = $(this).find("td:eq(4)");
{
value.text(price*amount);
sum += price*amount;
} else {
value.text(0);
}
});
$("#summary").text(sum);
}
$(document).ready(function(){
update();
$("#calculator input").keyup(function(){
update();
});
});
</script>
</head>
<body>
<table id="calculator">
<thead>
<tr>
<th>Rodzaj usługi</th>
<th>Jednostki</th>
<th>Cena</th>
<th>Ilość</th>
<th>Wartość</th>
</tr>
</thead>
<tfoot>
<tr>
<th colspan="4">Razem</th>
<th id="summary"></th>
</tr>
</tfoot>
<tbody>
<tr>
<td>Produkt 1</td>
<td>1 szt.</td>
<td><input type="text" name="cena1"></td>
<td><input type="text" name="produkt1"></td>
<td></td>
</tr>
<tr>
<td>Produkt 2</td>
<td>1 szt.</td>
<td><input type="text" name="cena2"></td>
<td><input type="text" name="produkt2"></td>
<td></td>
</tr>

</tbody>
</table>
</body>

Co w powyzszym kodzie jest zle ze nie dziala?
b4rt3kk
Ja bym to prościej zrobił:

  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <title>Tytul</title>
  4. <meta charset="utf-8" />
  5. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
  6. <script type="text/javascript">
  7. $(function(){
  8. $('input').keyup(function(){
  9. var quantity = $(this).closest('.row').children('.cell').children('.quantity').val();
  10. var price = $(this).closest('.row').children('.cell').children('.price').val();
  11. sum = quantity*price;
  12. $(this).closest('.row').children('.cell').children('.sum').html(sum+' PLN');
  13. });
  14. });
  15. <style type="text/css">
  16. .table {
  17. display: table;
  18. }
  19.  
  20. .row {
  21. display: table-row;
  22. }
  23.  
  24. .odd {
  25. background: silver;
  26. }
  27.  
  28. .cell {
  29. display: table-cell;
  30. padding: 5px;
  31. }
  32.  
  33. .row input {
  34. width: 100px;
  35. }
  36. </head>
  37.  
  38. <div class="table">
  39. <div class="row">
  40. <div class="cell">Produkt 1</div>
  41. <div class="cell"><input class="quantity" type="text" /></div>
  42. <div class="cell"><input class="price" type="text" /></div>
  43. <div class="cell"><span class="sum"></span></div>
  44. </div>
  45. <div class="row odd">
  46. <div class="cell">Produkt 2</div>
  47. <div class="cell"><input class="quantity" type="text" /></div>
  48. <div class="cell"><input class="price" type="text" /></div>
  49. <div class="cell"><span class="sum"></span></div>
  50. </div>
  51. <div class="row">
  52. <div class="cell">Produkt 3</div>
  53. <div class="cell"><input class="quantity" type="text" /></div>
  54. <div class="cell"><input class="price" type="text" /></div>
  55. <div class="cell"><span class="sum"></span></div>
  56. </div>
  57. <div class="row odd">
  58. <div class="cell">Produkt 4</div>
  59. <div class="cell"><input class="quantity" type="text" /></div>
  60. <div class="cell"><input class="price" type="text" /></div>
  61. <div class="cell"><span class="sum"></span></div>
  62. </div>
  63. </div>
  64. </body>


O to chodzi?
-poczatkujacy-
Fakt, inaczej ale efekt ten sam

Brakuje tylko sumy calosci - bo liczy poszczegolne produkty

Jak zmienic aby mozna bylo podawac kwoty 2,50 a nie 2.50.
Jak sie wpisze 2,50 pojawia sie "NaN"

Z reszta sobie poradze wink.gif
b4rt3kk
Musisz użyć replace, bo amerykańce używają kropek jako delimitera i tak się przyjęło w językach programowania.

  1. var price = $(this).closest('.row').children('.cell').children('.price').val().replace(',','.');


Co do sumy całości:

  1. var total = 0;
  2. $('.sum').each(function(){
  3. total += $(this).val();
  4. });
-poczatkujacy-
Dzieki.

var price - podmieniam linijke
gdzie wsawic 2 kod var total = 0;
i jak go wywolac?

-poczatkujacy-
Nadal nie moge tego poskladac sad.gif
pomozesz?

Chcialem "podziekowac" ale chyba niezalogowani nie moga, bo nie widze przycisku.
Zaraz zaloze konto.
b4rt3kk
Proszę, poprawiona wersja skryptu:

  1. <!DOCTYPE html>
  2. <html lang="pl">
  3. <title>Tytul</title>
  4. <meta charset="utf-8" />
  5. <script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
  6. <script type="text/javascript">
  7. $(function(){
  8.  
  9. $('input').keyup(function(){
  10. var quantity = $(this).closest('.row').children('.cell').children('.quantity').val();
  11. var price = $(this).closest('.row').children('.cell').children('.price').val();
  12. sum = quantity*price;
  13. $(this).closest('.row').children('.cell').children('.sum').html(sum);
  14. Total();
  15. });
  16.  
  17. function Total() {
  18. var total = 0;
  19. $('.sum').each(function(){
  20. if ($(this).html() == '') total += 0; else total += parseInt($(this).html());
  21. });
  22. $('.total').html(total);
  23. }
  24.  
  25. });
  26. <style type="text/css">
  27. .table {
  28. display: table;
  29. }
  30.  
  31. .row {
  32. display: table-row;
  33. }
  34.  
  35. .odd {
  36. background: silver;
  37. }
  38.  
  39. .cell {
  40. display: table-cell;
  41. padding: 5px;
  42. }
  43.  
  44. .row input {
  45. width: 100px;
  46. }
  47. </head>
  48.  
  49. <div class="table">
  50. <div class="row">
  51. <div class="cell">Produkt 1</div>
  52. <div class="cell"><input class="quantity" type="text" /></div>
  53. <div class="cell"><input class="price" type="text" /></div>
  54. <div class="cell"><span class="sum"></span></div>
  55. </div>
  56. <div class="row odd">
  57. <div class="cell">Produkt 2</div>
  58. <div class="cell"><input class="quantity" type="text" /></div>
  59. <div class="cell"><input class="price" type="text" /></div>
  60. <div class="cell"><span class="sum"></span></div>
  61. </div>
  62. <div class="row">
  63. <div class="cell">Produkt 3</div>
  64. <div class="cell"><input class="quantity" type="text" /></div>
  65. <div class="cell"><input class="price" type="text" /></div>
  66. <div class="cell"><span class="sum"></span></div>
  67. </div>
  68. <div class="row odd">
  69. <div class="cell">Produkt 4</div>
  70. <div class="cell"><input class="quantity" type="text" /></div>
  71. <div class="cell"><input class="price" type="text" /></div>
  72. <div class="cell"><span class="sum"></span></div>
  73. </div>
  74. <div class="row">
  75. <div class="cell"><span class="total"></span></div>
  76. </div>
  77. </div>
  78. </body>
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.