Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Problema each e somme campi

    ciao!

    io ho questi campi (ne posso anche aggiungere N-altri, quindi non sono solo questi):
    codice:
    <input type="text" name="imp_voce[]" value="100" class="form-control imp_voce"
    placeholder="Importo" autofocus
           required><input type="text" name="sconto_voce[]" value="20" class="form-control sconto_voce"
    placeholder="Sconto"
    required>
    
    <input type="text" name="imp_voce[]" value="0" class="form-control imp_voce"
    placeholder="Importo">
    
    <input type="text" name="sconto_voce[]" value="0" class="form-control sconto_voce"
    placeholder="Sconto">


    in pratica dovrei calcolare l'importo per oguno, considerando lo sconto.
    questa la funzione:
    codice:
    function calcolaSomma() {
        var totImporto = 0;
        let importo = $('.imp_voce');
        let sconto = $('.sconto_voce');
    
        importo.each(function (i) {
            var outerThis = $(this);
            if (outerThis.val() > 0) {
                totImporto += parseInt(outerThis.val());
                sconto.each(function (i) {
                    var innerThis = $(this);
                    if (innerThis.val() > 0) {
                        console.log(innerThis.val());
                        totImporto -= (totImporto * innerThis.val() / 100);
                    }
                });
            }
        });
    
        console.log(totImporto);
    }
    tutto bene finchè riempio solo i primi due campi:
    codice:
    IMP1 = 100
    SC1 = 20
    IMP2 = 0
    SC2 = 0
    
    TOTALE = 80
    ma già se riempio il secondo campo importo, il calcolo sballa:
    codice:
    IMP1 = 100
    SC1 = 20
    IMP2 = 100
    SC2 = 0
    
    TOTALE = 144 invece di 180
    questo perchè ricalcola anche SC1, invece di considerare lo sconto 0.
    perchè???

  2. #2
    dovrei aver risolto così:
    codice:
    function calcolaSomma() {
        var totImporto = 0;
        let importo = document.getElementsByClassName('imp_voce');
        let sconto = document.getElementsByClassName('sconto_voce');
    
        for (var i = 0; i < importo.length; i++) {
            var localImp = 0;
            var v = importo[i].value;
            if (v > 0) {
                localImp += parseInt(v);
                var s = sconto[i].value;
                if (s > 0) {
                    localImp -= (localImp * s / 100);
                }
            }
            totImporto += localImp;
        }
        console.log(totImporto);
    }
    - ho usato il classico for loop invece dell'each di jquery
    - non ho iterato sul secondo array, ma sono andato a prendere il valore dall'elemento corrente
    - ho corretto anche un errore di logica sul calcolo totale, usando una variabile "temporanea"

    sembrerebbe funzionare.
    ovviamente si accettano suggerimenti.


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 © 2024 vBulletin Solutions, Inc. All rights reserved.