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è???