Code:var summe_farbe[1] = 0; $('input[name~="produkt"]).each(function(){ // Differentiate colors here // Sum up prices if($.isNumeric($(this).val()) summe_farbe[1] += $(this).val(); });
Ergebnis 1 bis 3 von 3
- 28.12.2011, 13:05 #1Jonagold
Themenstarter
- Registriert
- 05.2010
- Beiträge
- 23
JavaScript/JQuery Summe von mehreren Inputs
Hallo,
ich bin nicht so der größte Javascript bzw. JQuery Experte, muss jetzt aber doch mal ein bisschen was darin machen. Allerdings fehlt mir gerade der Ansatz, wie ich das am besten Löse.
Ich habe eine Reihe von Input Feldern für Produkte, die nach folgendem Schema aufgebaut sind:
<input type="text" name="produkt[(int)ID][(int)FARBE][(int)GROESSE]" value=""> --> Anzahl
Es gibt mehrere Produkte, die jeweils mehrere Farben haben können.
Eine Farbe kann wiederum mehrere Größen haben, für die jeweils eine Anzahl eingegeben werden kann.
Wie kann ich jetzt die Gesamtsumme für die Anzahl innerhalb einer Farbe rausfinden?
Beispiel:
Produkt1 - Farbe 2 - Größe 20 => 5
Produkt1 - Farbe 2 - Größe 21 => 7
Produkt1 - Farbe 2 - Größe 22 => 3
--> Summe = 15
Bzw:
<input type="text" name="[2][6][15]" value="2">
<input type="text" name="[2][6][18]" value="2">
<input type="text" name="[2][6][19]" value="4">
--> Summe = 10
Wäre super, wenn mir da jemand einen Ansatz liefern könnte!
LG && Danke
- 28.12.2011, 13:43 #2
- 28.12.2011, 14:17 #3Jonagold
Themenstarter
- Registriert
- 05.2010
- Beiträge
- 23
Danke!
Hab mir mit einer Inspiration von Stackoverflow auch gerade was zusmamengebaut, was einfach alle inputs in einer TR scannt, die nicht ausgeschlossen sind. Vielleicht hilft es ja auch nochmal jemandem:
Code:$(document).ready(function(){ $("input").each(function() { var that = this; // fix a reference to the <input> element selected $(this).keyup(function(){ newSum.call(that); // pass in a context for newsum(): // call() redefines what "this" means // so newSum() sees 'this' as the <input> element }); }); }); function newSum() { var sum = 0; var anzahl = 0; var thisRow = $(this).closest('tr'); thisRow.find('td:not(.rechner) input:text').each( function(){ anzahl = parseFloat(this.value); if(isNaN(anzahl)){ anzahl = 0;} // Keine Zahl = 0 sum += anzahl; }); gesamt = thisRow.find('td.rechner input:text.qty').val(sum).val(); preis = thisRow.find('td.rechner input:hidden').val(); ordereurgesamt = gesamt*parseFloat(preis); thisRow.find('td.rechner input:text.ordereurgesamt').val(ordereurgesamt); }


Zitieren
