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

Javascript 当使用ajax刷新数据时,Input.value变得未定义,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我每3秒钟更新一次数据,但由于某种原因,它将'btc'和'cur'的input.value都变为未定义。 下面是更新价格的代码 $(function() { getVars(); }); function getVars() { $.getJSON('getPrice.php', function(data) { //getting vars from file currentPrice = data; var btc = document.getElementById(

我每3秒钟更新一次数据,但由于某种原因,它将'btc'和'cur'的input.value都变为未定义。 下面是更新价格的代码

$(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:无法读取未定义的属性“length”。由于某些原因,当我刷新价格时,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>

坐
一点
mBTC
BTC

=


另外,您使用
accounting.unformat(btc.value)
作为参数调用函数,然后在函数中使用该参数作为HTML输入,但是
accounting.unformat(btc.value)
以某种方式返回元素,然后?
input.value
未定义,可能是因为
input
不是你想象的那样
input
作为参数从任何调用它的地方传递到函数中,在html中传递
btc
任何应该是的内容,但假设它转换为
window.btc
,并且您得到具有该ID的元素(糟糕的操作方式),但是当你从代码中调用它时,你会传入
accounting.unformat(btc.value)
,它显然会返回数字,为什么这些数字会有
value
属性,或者像输入元素一样?@Darkstar在你的btcConvert()函数中,
input.value
是顶部输入框中的值,我可以看到。至少当您更改它的值时,您调用该函数时使用
this
作为参数,该参数在该函数中采用
input
var名称。让我们从顶部开始,将函数调用为
btcConvert(accounting.unformat(btc.value))
,其中
btc
是一个元素,从
unformat
函数返回的值是一个数字等。您将该数字作为
input
传递给函数,此处
函数btcConvert(input){
,其中input是您刚刚发送给函数的数字。然后您执行
input.value
,但是
input
已经是值了,它甚至是“未格式化的”,所以您无法再次获得值?应该是这样,但正如@adeneo所说,您必须调用
btcConvert(accounting.unformat(btc));
(或
cur
)(不带
.value
),因为您在函数中再次请求
.value