输出中未定义/ NaN错误

Pra*_*Rai 5 javascript numerical nan undefined

这是我在stackoverflow上的第一篇文章,我遇到以下代码中的错误,在firefox中的inspect元素/ JS控制台中没有显示错误,但由于某种原因,计算后的输出显示未定义/ NaN错误.用户的输入在Float中解析.

码:

function costtoShip(){

    // get input 

    var weight = parseFloat(document.getElementById("weight")).value ;
    var msg;
    var cost;
    var subtotal;

    // calculation

    if ( weight >= 0.00 && weight <= 150.00 ) {
        cost = 20.00;
    }

    else if ( weight >= 151.00 && weight <= 300.00 ) {
        cost = 15.00;
    }

    else if ( weight >= 301.00 && weight <= 400.00 ) {
        cost = 10.00;
    }    


   subtotal = weight * cost ;
   msg = "<div> Total Weight is " + weight + "</div>" ;
   msg = msg + "<div> Subtotal is " + subtotal + "</div>" ;


   // send output

    document.getElementById("results").innerHTML = msg ;
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*her 7

var weight = parseFloat(document.getElementById("weight")).value;
Run Code Online (Sandbox Code Playgroud)

这不是parseFloat的方法.你试图value在一个双重调用属性.移出括号.

var weight = parseFloat(document.getElementById("weight").value);
Run Code Online (Sandbox Code Playgroud)

我建议console.log(weight);您在代码周围使用或使用其他值,这样您就可以更轻松地指出这些问题.