Reputation: 43
I have a problem with my code.
$('input[name="pocet"]').each(function(){
var $mnozstvo = parseInt($('this').val());
var $id = parseInt($(this).attr('id').substring(6));
var $aktualnyProdukt = $('#' + $id);
var $povodnaCena = parseFloat($aktualnyProdukt.data('price'));
var $riadkoveZlavy = $aktualnyProdukt.find('div .vypocetPreZlavu');
var $aktualnaCena = $povodnaCena;
if($riadkoveZlavy.length > 0) {
$riadkoveZlavy.each(function() {
$mnozstvoNaZlavu = parseInt($(this).data('mnozstvo'));
$cenaPoZlave = parseFloat($(this).data('cena'));
if($mnozstvo >= $mnozstvoNaZlavu) {
$aktualnaCena = $cenaPoZlave;
}
});
}
if(isNaN($mnozstvo)) $mnozstvo = 0;
total += $mnozstvo * $aktualnaCena;
});
However, after running this function, total returns NaN and I have no idea why. Could you help me?
HTML: http://jsfiddle.net/UL7Sr/
Upvotes: 1
Views: 154
Reputation: 43
var $povodnaCena = parseFloat($aktualnyProdukt.data('price'));
var $povodnaCena = parseFloat($aktualnyProdukt.attr('data-price'));
Replacing the first line with the second one solved my problem.
Upvotes: 0
Reputation: 4842
var $mnozstvo = parseInt($('this').val());
should actually be var $mnozstvo = parseInt($(this).val());
Additionally, make sure that $aktualnaCena
is a number. You do this for $mnozstvo
, but not for $aktualnaCena
. Try:
if(isNaN($mnozstvo)) $mnozstvo = 0;
if(isNaN($aktualnaCena)) $aktualnaCena = 0;
total += $mnozstvo * $aktualnaCena;
Also, please don't prefix all your variables with a $. JavaScript is not PHP. The convention when using jQuery is that you do that for jQuery element variables to distinguish from other variables. If you use it for all your variables in that context, it is actually confusing.
Upvotes: 4