可能重复:
When I press enter I get isNaN, but the value is a number如何改变一个变量的内容,以零,如果isNaN
我把我的功课最后Sunday.It被送回我要纠正,因为isNaN值被返回到Total文本框中。我认为这是编程要做的事情。相反,据她说,“isNaN函数将检查变量并确定变量是否包含非数字数据,然后下一个语句会将变量的内容更改为零,”这让我感到困惑。任何建议这里是代码。 *回报;”在进行更改之前,语句不是语法错误。这里是链接到上传的文件:ciswebs.smc.edu/cis54/tyson_schweidel/homework2.htm
<!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");
return; //gets syntax error
}
var tax = (numPrice * taxP)/100;{
var total = numPrice + tax;
numPrice = 0;
taxP = 0;
document.getElementById("total").value = "$" + total.toFixed(2);
return; //gets syntax error
}
</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:
<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>
你需要从if嵌套中取出if(isNaN(taxP))条件,否则它将不会捕获错误,除非numPrice '也不是一个数字。您还需要在返回之前将该字段设置为0。 – bdares
设置字段为0是什么意思?什么字段,如return = 0。 – swydell