JavaScript returns NaN even if input is an integer

后端 未结 1 491
粉色の甜心
粉色の甜心 2021-01-28 10:41

I am typing an integer in the input box and it still returns \'NaN\'.



        
1条回答
  •  后悔当初
    2021-01-28 11:33

    document.getElementById("sidea") doesn't returns a number, but an element. Use document.getElementById("sidea").value instead to get the element value.

    So:

    function goldenFunctiona() {
        var sideaVal = document.getElementById("sidea").value;
    
        var a = parseInt(sideaVal , 10); // Get parseInt's return value or 'a' will still be a string
        var b = ((a + a) * (Math.pow(5, 0.5))) / 2;
    
        document.getElementById("b").innerHTML = b;
    }
    

    0 讨论(0)
提交回复
热议问题