Javascript jQuery-触发单击事件时出现奇怪错误
以下是我的JavaScript代码:Javascript jQuery-触发单击事件时出现奇怪错误,javascript,jquery,Javascript,Jquery,以下是我的JavaScript代码: function GetPriceForChildren(Price) { if(ChildrenItem == 1){ var OnePercent = Price / 100; var TwentyPercent = 20 * OnePercent; var FinalPrice = Price + TwentyPercent; FinalPrice = FinalPrice.to
function GetPriceForChildren(Price) {
if(ChildrenItem == 1){
var OnePercent = Price / 100;
var TwentyPercent = 20 * OnePercent;
var FinalPrice = Price + TwentyPercent;
FinalPrice = FinalPrice.toFixed(2)
return FinalPrice;
}
if(ChildrenItem == 0){
return Price;
}
}
$('.ColorImages').each(function() {
$(this).click(function() {
$(".ColorImages").removeClass("SelectColor");
$( ".ColorImages" ).removeClass("ColorImagesSelected");
$( this ).addClass( "ColorImagesSelected" );
var ImageLink = $(".ColorImages.ColorImagesSelected").attr( "imageLink" );
var SelectedColorName = $(".ColorImages.ColorImagesSelected").attr( "alt" );
$("#PRODUCTCOLOR").val(SelectedColorName);
$('#AnimationRotator').attr('data','http://www.sportsdirect.com/images/images360/' + ImageLink + '.swf');
var ColorNumber = $( this ).attr( "imageLink" );
$('#LoadingImage').show();
$.ajax({
url: 'CheckColorPrice.php',
type: 'POST',
data: {
url: '<?php echo $LINK;?>',
ColorId: ColorNumber
},
dataType: 'json',
success: function (data) {
$("#PRICE").text("£ " + GetPriceForChildren(data["price"]));
var lev = <?PHP echo $PoundCurrency; ?> * GetPriceForChildren(data["price"]);
lev = lev.toFixed(2);
$("#lev").text(lev + " лв.");
$("#PRODUCTPRICE").val(lev);
$('#SizeOfProduct').find('option').remove().end().append('<option selected="selected" value="">Please select</option>');
var AutoIncreasment = 0;
$.each(data.sizenames, function(index, sizenames) {
$('#SizeOfProduct').append('<option value="'+ sizenames +'" size-number="'+ (AutoIncreasment++) +'">'+ sizenames +'</option>');
});
$('.ColorImagesNOColor').fadeOut();
$("#Broi").val("1");
$('#LoadingImage').hide();
}
});
});
});
我完全不明白这一点。我想知道错误在哪里,似乎几个小时前我还找不到。
你能告诉我我的错误在哪里,我怎样才能改正它吗
提前谢谢 数据[“价格”]
不是数字,可能是字符串。试着做
$("#PRICE").text("£ " + GetPriceForChildren(parseFloat(data["price"])));
var lev = <?PHP echo $PoundCurrency; ?> * GetPriceForChildren(parseFloat(data["price"]));
$(“#价格”).text(“£”+GetPriceForChildren(parseFloat(数据[“价格])));
var-lev=*GetPriceForChildren(parseFloat(数据[“价格]));
parseFloat将字符串转换为十进制数。只需在
FinalPrice=FinalPrice.toFixed(2)
之前放置一个console.log(FinalPrice)
,看看你得到了什么。它是未定义的还是空的?在GetPriceForChildren
函数中添加一个断点,逐步检查代码并检查值(类型、值…)@palash I get console1.000.2
。因为所有计算都基于硬编码数字或以前的计算,除了price
,我想,price
没有像您期望的那样进入函数。在函数顶部添加console.log(price)代码>作为第一行,并查看作为参数传递的值。就是这样!我可以在函数中进行此对话吗?请确保在开始时添加Price=parseFloat(Price)
,您应该准备好进行典型的对话。出于谨慎考虑,最好将基数包含在parseFloat
(即parseFloat(Price,10);
…同意。我只是想保持简单
$("#PRICE").text("£ " + GetPriceForChildren(parseFloat(data["price"])));
var lev = <?PHP echo $PoundCurrency; ?> * GetPriceForChildren(parseFloat(data["price"]));