I have a page (here you can see it) in which if i click on a checkbox I need to sum its price to actual price.
The checkboxes have a value
and a data-prezzo
. those two numbers need to sums to two different divs like that:
value
+ rata-display-1
= total_rata
data-prezzo
+ prezzo-somma
= total_prezzo
I'm new so i don't know why it gave me NaN instead the correct sum.
here's the code:
//sommo rata e prezzo dei plus a quelli del DB
var basicPrice_rata = 1000 ; // This is how we start
var basicPrice_prezzo = 50000 ; // This is how we start
$(document).on('change', getCheck);
function getCheck() {
var currentPrice_rata = basicPrice_rata; // every time
CurrentPrice_rata = basicPrice_rata;
var currentPrice_prezzo = basicPrice_prezzo; // every time
CurrentPrice_prezzo = basicPrice_prezzo;
plus = [],
total_rata = 1000;
total_prezzo = 50000;
$("input[type=checkbox]").each(function(i, el) {
if ($(el).is(":checked")) {
total_rata += parseInt($(el).val());
total_prezzo += parseInt($(el).data('prezzo'));
console.log(total_rata);
plus.push($(el).data('name')); // get the label text
}
});
//inserisco i nomi dei plus nella riga di testo
$("#plus-display").text(plus.join(", "));
//inserisco le rate sommate nei div corrispondenti
$('#rata-display-2').text(total_rata +" €");
//inserisco i prezzi sommati nei div corrispondenti
$('#prezzo-somma').text(total_prezzo +" €");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" value="134.24" data-prezzo="40000" id="box-auto" name="Box auto" data-name="Box auto" class="check-plus w-checkbox-input">
<input type="checkbox" value="17.78" data-prezzo="5000" id="posto-moto" name="Posto moto" data-name="Posto moto" class="check-plus w-checkbox-input">
<input type="checkbox" value="134.24" data-prezzo="40000" id="box-auto" name="Box auto" data-name="Box auto" class="check-plus w-checkbox-input">
<div id="rata-display-2" class="prezzo-checkout">1000 €</div>
<div id="prezzo-somma" class="text-checkout color bold">500000 €</div>
<div id="plus-display" class="text_piccolo black checkout">Interior Pack Incluso nel prezzo</div>
Aucun commentaire:
Enregistrer un commentaire