2012-11-01 101 views
0

我越来越NaN的结果,并且其怎么一回事,因为我的jQuery是倍增,看起来像"204,3 * 3"我在我的jQuery的结果给出的NaN

我该如何处理它的数字?

我不能改变价格,那我该怎么办?

"234 * 2"只要号码有','就可以正常工作我收到NaN

<script type="text/javascript"> 
    $('.quantity').keyup(function() { 
     var parent = $(this).parent(), 
      price = parent.find('.price').html(), 
      quantity = parent.find('.quantity').val(), 
      result = price * quantity; 
     parent.find('.price2').html(result); 
    }); 
</script> 

    <span class="price">69,9</span> 
    <input type="text" class="quantity">  
    <span class="price2">69,9</span> 
    <span class="total">Total:</span> 
    <div class="line2"></div> 

Check my JSfiddle everything is there

任何形式的帮助表示赞赏

+1

欢迎在此发布代码。问题应该是自成体系的,我们不希望依赖外部网站永远可用,以保持问题的有用性。 – Collin

+2

改为使用'204.3'。另外,如果你打算使用它们,通常最好将东西转换成浮动。 – Brad

+0

Javascript有逗号问题。看到这个问题:http://stackoverflow.com/questions/3205730/javascript-parsefloat-500-000-returns-500-when-i-need-500000 – Laurence

回答

1

你试图乘串...使用parseFloat()和REPLACE()方法,如在你的jsfiddle更新here

$('.quantity').keyup(function() { 
    var parent = $(this).parent(), 
     price = parent.find('.price').html().replace(',', '.'), 
     quantity = parent.find('.quantity').val().replace(',','.'), 
     result = parseFloat(price) * parseFloat(quantity); 
    parent.find('.price2').html(result); 
}); 
+0

如何使用parseInt()去帮助分数?! –

+1

哦,对不起,parseFloat()?更新的代码和小提琴以显示更改 –

+0

有时它会给出像“207,700000000007 –

1

您是乘以两个字符串这里,而不是数字..

转换他们使用parseInt函数与基数

OR

使用parseFloat

更改这条线将它们转换

result = price * quantity; 

TO

result = parseInt(price,10) * parseInt(quantity,10); 

OR

result = parseFloat(price) * parseFloat(quantity); 
+0

谢谢,parseFloat伟大的作品 –

+0

@AlianaRose ...很高兴能提供帮助:) –

+0

@AlianaRose - 仔细检查你的数学运算,确保你得到了你期望的结果,''parseFloat()'方法将**从你的输入中去掉'3,''parseFloat (“10,3”)=== 10' –

4

JavaScript使用北美数字格式,这意味着,用作thousands seperator.使用decimal separator

有两种解决问题的方法:

  • 教你的用户喜欢1000.25
  • 输入数字写一个程​​序来把1.000,251000.25

String.prototype.replace将是你的朋友第二选择。