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



这是一个问题,我不明白,因为所有的代码验证。是我今晚午夜要交的作业。

当我在"Price"文本框中输入一个值并按回车键时,我在"Total"文本框中得到$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>

我想你忘记关闭你的{},你应该关闭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;
}

try this:

<!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>

disabled="disabled();"可能是一个问题—将其设置为只读,并将税设置为只读。

您可以根据价格算出税金和总额。

你的代码在第一次改变输入时读取一个空值(NaN)。

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

最新更新