Nun, ich habe diese Texteingabe, die Summe aller Attribute von "Preis" in allen Checkboxen wird überprüft. jetzt kann ich keinen Attributwert von einer Checkbox erhalten.Wie kann ich Attributwert von einem Kontrollkästchen erhalten, wenn es aktiviert ist?
hier ist meine Funktion:
function sum_options() {
var options = [];
$("#form-field-1-11-1").attr("price", 500);
$("#form-field-1-11-2").attr("price", 500);
$("#form-field-1-11-3").attr("price", 0);
$("#form-field-1-11-4").attr("price", 300);
$("#form-field-1-11-5").attr("price", 500);
$("#form-field-1-11-6").attr("price", 500);
$("#form-field-1-11-7").attr("price", 1250);
$("#form-field-1-11-8").attr("price", 500);
$("#form-field-1-11-9").attr("price", 700);
options[0] = $("#form-field-1-11-1");
options[1] = $("#form-field-1-11-2");
options[2] = $("#form-field-1-11-3");
options[3] = $("#form-field-1-11-4");
options[4] = $("#form-field-1-11-5");
options[5] = $("#form-field-1-11-6");
options[6] = $("#form-field-1-11-7");
options[7] = $("#form-field-1-11-8");
options[8] = $("#form-field-1-11-9");
var total = 0;
$.each(options, function() {
this.on("change", function() {
total += this.attr("price");
});
});
$("#sum-field").val(total);
}
Dank !!!
Beachten Sie, dass innerhalb von 'each()' 'this' ist ein DOMElement welches nicht' on() 'und' attr() ' Methoden; Sie müssen es in ein jQuery-Objekt konvertieren, bevor Sie diese Methoden mit $ (this) aufrufen. Siehe meine Antwort unten für eine umfassendere Antwort, die auch die Logik verbessert, die Sie verwenden. –