使用ajax刷新数据时,Input.value变为未定义

时间:2015-12-29 21:40:46

标签: javascript php jquery ajax

我每隔3秒钟更新一次数据,但出于某种原因,它正在转变“btc”和“btc”的输入值。并且' cur'未定义。 以下是更新价格的代码

$(function() {
    getVars();
});
function getVars() {
$.getJSON('getPrice.php', function(data) { //getting vars from file
    currentPrice = data;
    var btc = document.getElementById("btc");
    var cur = document.getElementById("cur");
    if (currentInput == "btc") {
        btcConvert(accounting.unformat(btc.value));
    } else {
        usdConvert(accounting.unformat(cur.value));
    }
    });
    setTimeout("getVars()",3000);                //Refresh vars every 3 sec
            };

以下是usdConvert和btcConvert

的代码
            var co2 = document.getElementById('btc');
            if (currentUnit == SAT) {
                var rounding = 0;                   
            } else if (currentUnit == BIT) {
                var rounding = 2;
            } else if (currentUnit == MBIT) {
                var rounding = 5;
            } else if(out2 == 0) {
                var rounding = 0;
            }else if(currentUnit == BTC) {
                var rounding = 8;
            }
            var trimmed = trim(accounting.unformat(accounting.formatNumber(out2, rounding)));
            btc.value = accounting.formatNumber(trimmed, decimalPlaces(trimmed));
        }

    If you inspect element you can see an error saying:

未捕获的TypeError:无法读取属性'长度'未定义的。出于某种原因,当我刷新价格时,input.value变得不确定。

编辑: 它可能与我如何在html中设置初始btc.value有关。

<center>
    <div class = "ratesBox">
    <div class = "bitcoin">
        <div class = "rateboxy"><input value = "1" type="text" name = "btc" id="btc" class="rate" onchange="btcConvert(this);" onkeyup ="btcConvert(this);"/></div>
    </div>
    <div class= "unitBox">
        <div class = "smallUnitBox" onclick="satoshiConvert(btc);" id="satoshiBox">sat</div>
        <div class = "smallUnitBox" onclick="bitConvert(btc);" id="bitBox">bit</div>
        <div class = "smallUnitBox" onclick="mBTCConvert(btc);" id="mBTCBox">mBTC</div>
        <div class = "smallUnitBox2" onclick="bitcoinConversion(btc);" id="BTCBox">BTC</div>
    </div>
            <p id = "equals">=</p>
        <div class = "rateboxy"><input value = "$<?php echo json_encode($bitcoinPrice); ?>" type="text" name="cur" id="cur" class="rate" onchange="usdConvert(this);" onkeyup ="usdConvert(this);"/></div>
    </div>
</center>
  <div id="chart_div"></div>

0 个答案:

没有答案