dongmibeng5885 2018-06-08 16:46
浏览 60

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

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

  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?