2014-10-17 52 views
1

任何想法为什么比特币值为0?用JQuery显示比特币值

我使用JSON检索数据:

jQuery.ajax({ 
    dataType: "json", 
    url: "https://bitpay.com/api/rates", 
    success: function(data) { ... } 

这里的小提琴:

http://jsfiddle.net/wL5jh7np/

+0

因为'num'是0,所以'0/380.41 = 0',什么是'num',你想做什么 – 2014-10-17 00:56:02

回答

1

你的问题就在这里:var tex = jQuery(this).text(); //this is empty!
然后你去关闭,这样做var num = Number(tex.replace(/[^0-9.-]+/g,""));//now num=0 然后你这样做......

text: ""+(num/data[member].rate).toFixed(4)+" BTC*" //0 times something ... always 0; 

所以要解决这个问题,你必须确保你阅读的价值实际上是有意义的......

http://jsfiddle.net/wL5jh7np/7/

+0

嗨,Birgit!我试图进一步说明这一点:http://jsfiddle.net/askhflajsf/ywx6L9zn/4/ - 但是你会碰巧知道为什么当你输入2 BTC的时候美元变得不合时宜? – 2017-01-09 02:57:43

+1

http://jsfiddle.net/ezb96y58/4/-看看这一个... – 2017-01-09 15:00:48

+0

绝对惊人的,非常感谢你! – 2017-01-09 15:13:06

1

你捣鼓好。在`span.pricedisplay'中输入一些数值,那么你可以看到结果

将结果看作0的原因(默认)是span.pricedisplay没有任何价值。如果我们看到了下面的计算num/data[member].rate它很清楚,NUM = 0空的文本,并将其结果将是0

 // Read original text from span.pricedisplay and this referes to that span 
     var tex = jQuery(this).text(); 

     // Convert text to a double 
     var num = Number(tex.replace(/[^0-9.-]+/g,"")); 

     // Create a new div with the class BTC_Price and append it after the original price 
     var n = jQuery('<div/>', { 
      class: 'BTC_Price', 
      text: ""+(num/data[member].rate).toFixed(4)+" BTC*" 
     }).insertAfter(jQuery(this));