Javascript或Jquery / PHP / Web Socket编号格式问题

我正在修改一个脚本并学习JS和Web套接字,同时将其改为我的需要。</ p> \ n

脚本是BTC和USD之间的简单计算器。</ p>

代码工作正常,但我无法获得正确的数字格式,因为我不确定我是否应该在JS中执行 或PHP。</ p>

HTML </ p>

 &lt; input type =“text”class =“form-control”id =“amtUSD”value  =“&lt;?php echo $ data ['price_usd'];?&gt;”&gt; 
</ code> </ pre>

JS </ p>

   var ember = []; 
var usdInput = document.getElementById(“amtUSD”);
var btcInput = document.getElementById(“amtBTC”);

ember.btcPrice =&lt;?php echo $ data [ 'price_usd']; ?&gt ;;
ember.socket = io.connect('https://socket.coincap.io');
ember.socket.on('trades',function(data){
var amt = document.getElementById (“amtUSD”);
if(data.coin ==“BTC”){

eter.amtUSD = usdInput.value;

eter.amtBTC = btcInput.value;

eter.usdCalc = ember.amtBTC * data.msg.price;
if(data.msg.price&gt; amt.value){
$(amt).addClass('increment');
} else {
$(amt).addClass( '递减');
}
$(“#amtUSD”)。attr(“value”,ember.usdCalc);
setTimeout(function(){
$(amt).removeClass('increment decrement' );
},700);
}
});

$(“#amtBTC”)。bind(“更改粘贴密钥”,函数(){
ember.usdCalc = $(this) .val()* ember.btcPrice;
$(“#amtUSD”)。attr(“value”,ember.usdCalc);
});
</ code> </ pre>

< 我试过用PHP。 它适用于第一个值,但只要它获得新值,它就会恢复为非格式化值。</ p>

 &lt;?php echo number_format($ data ['price_usd'  ],2);  ?&gt; 
</ code> </ pre>

所以,我认为我需要在JS或Jquery中格式化数字 - 这对于这种情况无关紧要,无论哪个更简单。</ p> \ n

提前致谢</ p>
</ div>

展开原文

原文

I'm modifying one script and learning JS and Web Sockets while changing it to my needs.

Script is a simple calculator between BTC and USD.

Code works fine, but I cant get proper number formatting since I'm not certain should I do it in JS or PHP.

HTML

<input type="text" class="form-control" id="amtUSD" value="<?php echo $data['price_usd']; ?>">

JS

var ember = [];
var usdInput = document.getElementById("amtUSD");
var btcInput = document.getElementById("amtBTC");

ember.btcPrice = <?php echo $data['price_usd']; ?>;
ember.socket = io.connect('https://socket.coincap.io');
ember.socket.on('trades', function(data) {
  var amt = document.getElementById("amtUSD");
    if (data.coin == "BTC") {
    ember.amtUSD = usdInput.value;
    ember.amtBTC = btcInput.value;
    ember.usdCalc = ember.amtBTC * data.msg.price;
    if(data.msg.price > amt.value) {
      $(amt).addClass('increment');
    } else {
      $(amt).addClass('decrement');
    }
    $("#amtUSD").attr("value", ember.usdCalc);
    setTimeout(function () {
        $(amt).removeClass('increment decrement');
    }, 700);
}
});

$("#amtBTC").bind("change paste keyup", function() {
ember.usdCalc = $(this).val() * ember.btcPrice;
$("#amtUSD").attr("value", ember.usdCalc);
});

I tried with PHP. It works for the first value, but as soon as it gets new value, it reverts to non formatted value.

<?php echo number_format($data['price_usd'], 2); ?>

So, I presume I need to format numbers in JS or Jquery - it doesnt matter for this case, whichever is simpler.

Thanks in advance

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐