计算导致$ NaN

时间:2017-06-09 19:35:14

标签: javascript

我认为我的问题在于解析" $"但我不确定如何解决它。这是代码:

https://jsfiddle.net/ottster/7oe9oy4a/

这可以" $"导致$ NaN的问题

$total = "$" + ($amount * $product); 

2 个答案:

答案 0 :(得分:0)

这是因为您将String与Number

相乘
$amount = "$" + $("#slider").slider("value");   //here result is in String format, like "$10"
$product = $("value"); //also there is no such path
$total = "$" + ($amount * $product); // "$10"*20 - results to "$Nan"

所以工作代码应该是这样的:

$amount = $("#slider").slider("value");
$product = $("#product").val();
$total = "$" + ($amount * $product);
....
$('#slider a').html('<label><span class="glyphicon glyphicon-chevron-left"></span> $' + $amount + ' <span class="glyphicon glyphicon-chevron-right"></span></label>');

答案 1 :(得分:0)

问题是$ amount是一个字符串=“$ somevalue”

 $amount = "$" + $("#slider").slider("value");

$ product是一个jQuery数组$product = $("value");

你试图将数组(对象)和字符串相乘,所以你会得到NAN