Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

javascript - When I press enter I get isNaN, but the value is a number

This is a problem that I don't understand because all the code validates. It's my homework that's due tonight at Midnight.

When I enter a value into the 'Price' text box and hit enter I get the $isNaN in the 'Total' textbox. Any suggestions. Here is the code:

<!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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you forget to close your { and }, and you should return to avoid calculating.

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;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...