当我按Enter键时,我得到isNaN,但值是一个数字

时间:2011-10-10 02:43:43

标签: javascript

这是一个我不明白的问题,因为所有代码都经过验证。这是我今晚午夜到期的作业。

当我在“价格”文本框中输入一个值并按Enter键时,我会在“总计”文本框中获得$ isNaN。有什么建议。这是代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4       /loose.dtd">
<html>
<head>
<title>Price Calculator</title>
<script type="text/javascript">
function fixOrder()
{
  var numPrice = parseFloat(document.getElementById("cost").value);
  var taxP = parseFloat(document.getElementById("tax").value);
  //var total = parseFloat(document.getElementById("total").value);

  if (isNaN(numPrice)){  
    alert("Sorry,you must enter a numeric value to place order");
    if (isNaN(taxP))
      alert("Sorry, you must enter a numeric tax value to continue");
  }

  var tax = (numPrice * taxP)/100;
  var total = numPrice + tax;
  document.getElementById("total").value = "$" + total.toFixed(2);
}
</script>

</head>

<body bgcolor="#00f3F1">

<h1 align="left">Price Calculator</h1>

<form name="form">
<p>
  Price:  <input type="text" id="cost" name="cost" value="" onchange="fixOrder();"/>
</p>
<p>
Tax: &nbsp; <input type="text" id="tax" name="tax" value="" onchange="fixOrder();"/>
</p>
<p>
  Total: <input type="text" id="total" name="total" value="" disabled="disabled();"/>
</p>
</form>

</body>
</html>

3 个答案:

答案 0 :(得分:2)

我认为您忘了关闭{},并且您应该return以避免计算。

if (isNaN(numPrice)) { 
    alert("Sorry,you must enter a numeric value to place order");
    return;
}

if (isNaN(taxP)) {
    alert("Sorry, you must enter a numeric tax value to continue");
    return;
}

答案 1 :(得分:1)

试试这个:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4       /loose.dtd">
<html>
<head>
<title>Price Calculator</title>
<script type="text/javascript">
function isNumber(value) { 
    return typeof value === 'number' &&
            isFinite(value);
}

function fixOrder()
{
    document.getElementById("total").value = "";  // clear the total field
    var numPrice = parseFloat(document.getElementById("cost").value);
    var taxP = parseFloat(document.getElementById("tax").value);
    //var total = parseFloat(document.getElementById("total").value);

    if (isNaN(numPrice)) { 
        alert("Sorry,you must enter a numeric value to place order");
        return;  // exit function to avoid calculation
    }
    if (isNaN(taxP)) {
        alert("Sorry, you must enter a numeric tax value to continue");
        return;  // exit function to avoid calculation
    }

    var tax = (numPrice * taxP)/100;
    var total = numPrice + tax;
    document.getElementById("total").value = "$" + total.toFixed(2);

 }

 </script>

 </head>

 <body bgcolor="#00f3F1">


<h1 align="left">Price Calculator</h1>

<form name="form">
<p>
Price:  <input type="text" id="cost" name="cost" value="" onchange="fixOrder();"/>
</p>
<p>
Tax: &nbsp;
<input type="text" id="tax" name="tax" value="" onchange="fixOrder();"/>
</p>
<p>
Total: 
<input type="text" id="total" name="total" value="" disabled="disabled"/>
</p>
</form>

</body>
</html>

答案 2 :(得分:0)

禁用= “禁用();”可能是一个问题 - 让它只读,税也。

您可以从价格中计算税金和总额。

第一次更改输入时,您的代码正在读取空值(NaN)。

您不需要名称,也不要为表单元素提供与其自己的处理程序中的变量相同的标识符 - 在某些浏览器中它们是全局变量。