Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it L'avatar di Scream
    Registrato dal
    Apr 2002
    Messaggi
    228

    [jquery] calculation e valore di default

    Devo fare una cosa apparentemente semplicissima con questa tabella che mi arriva da un form:

    Prodotto | quantità | pr unitario | totale
    testo | input | testo | testo

    Ho quindi usato jquery calculation che in pratica moltiplica il valore nell'input name quantità per il testo (id) del prezzo unitario per riempire l'id del totale

    codice:
        jQuery("[id^=total]").calc(
          // the equation to use for the calculation
          "qty * price",
          // define the variables used in the equation, these can be a jQuery object
          {
            qty: jQuery("input[name^=qt_]"),
            price: jQuery("[id^=prezzo_]")
          },
          // define the formatting callback, the results of the calculation are passed to this function
          function (s){
            // return the number as a dollar amount
            return "€ " + s.toFixed(2);
          },
          // define the finish callback, this runs after the calculation has been complete
          function ($this){
            // sum the total of the $("[id^=total_item]") selector
            var sum = $this.sum();
            
            jQuery("#grandTotal").text(
              // round the results to 2 digits
              "€ " + sum.toFixed(2)
            );
          }
        );
    Il problema è questo: i prodotti con valore 1 nell'input vengono poi calcolati ad ogni aggiornamento, quelli con valore 0 nell'input non vengono più calcolati.
    Se fossero tutti a 1 posso fare quello che voglio, ma ovviamente se un utente ha scelto 0 quantità di un prodotto non posso di certo metterlo a 1.

    Qualche idea in proposito?

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Ma se è questo, non mi sembra dia problemi al limite usa quello.
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it L'avatar di Scream
    Registrato dal
    Apr 2002
    Messaggi
    228
    è quello, ma non capisco... effettivamente ora non funziona proprio.
    provo a debuggare un attimo e al limite tra 5 minuti sono di nuovo qua...

  4. #4
    Utente di HTML.it L'avatar di Scream
    Registrato dal
    Apr 2002
    Messaggi
    228
    niente da fare. questo il sorgente:
    codice:
    <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.calculation.min.js"></script>
    <script type="text/javascript">
      var bIsFirebugReady = (!!window.console && !!window.console.log);
      $("[id^=total_item]").calc(
      // the equation to use for the calculation
      "qty * price",
      // define the variables used in the equation, these can be a jQuery object
      {
        qty: $("input[name^=qty_item_]"),
        price: $("[id^=price_item_]")
      },
      // define the formatting callback, the results of the calculation are passed to this function
      function (s){
        // return the number as a dollar amount
        return "$" + s.toFixed(2);
      },
      // define the finish callback, this runs after the calculation has been complete
      function ($this){
        // sum the total of the $("[id^=total_item]") selector
        var sum = $this.sum();
        
        $("#grandTotal").text(
          // round the results to 2 digits
          "$" + sum.toFixed(2)
        );
      }
    );
    </script>
    
    <script>
      document.createElement('article');
      document.createElement('section');
      document.createElement('hgroup');
      document.createElement('header');
      document.createElement('footer');
    </script>
    </head>
    
    <body>
    <table class="sortable" width="100%">
      <thead>
      <tr>
        <th scope="col">Prodotto</th>
        <th scope="col">Quantita</th>
        <th scope="col">Prezzo</th>
        <th scope="col">Totale &euro;</th>
      </tr>
      </thead>
    
      <tbody>
    <tr><td>Prodotto A</td><td><input type="text" value="1" name="qty_item_1" id="qty_item_1" size="3"></td><td id="price_item_1">43.5</td><td id="total_item_1">43.5</td></tr>
    <tr><td>Prodotto B</td><td><input type="text" value="1" name="qty_item_2" id="qty_item_2" size="3"></td><td id="price_item_2">43.5</td><td id="total_item_2">43.5</td></tr>
    <tr><td>Prodotto C</td><td><input type="text" value="1" name="qty_item_3" id="qty_item_3" size="3"></td><td id="price_item_3">43.5</td><td id="total_item_3">43.5</td></tr>
    <tr><td>Prodotto D</td><td><input type="text" value="1" name="qty_item_4" id="qty_item_4" size="3"></td><td id="price_item_4">43.5</td><td id="total_item_4">43.5</td></tr>
    <tr><td colspan="3">TOTALE</td><td style="text-align: right" id="grandTotal">0,00</td>
      </tbody>
    </table>

  5. #5
    Utente di HTML.it L'avatar di Scream
    Registrato dal
    Apr 2002
    Messaggi
    228
    Risolto, avevo dimenticato un pezzo di codice...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.