Javascript 当我按enter键时,我得到的是NaN,但值是一个数字
这是一个我不理解的问题,因为所有的代码都进行了验证。我的作业今晚午夜到期 当我在“价格”文本框中输入一个值并按enter键时,我在“总计”文本框中得到$isNaN。任何建议。代码如下:Javascript 当我按enter键时,我得到的是NaN,但值是一个数字,javascript,Javascript,这是一个我不理解的问题,因为所有的代码都进行了验证。我的作业今晚午夜到期 当我在“价格”文本框中输入一个值并按enter键时,我在“总计”文本框中得到$isNaN。任何建议。代码如下: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4 /loose.dtd"> <html> <head> <title>Pr
<!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: <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>
物价计算器
函数fixOrder()
{
var numPrice=parseFloat(document.getElementById(“成本”).value);
var taxP=parseFloat(document.getElementById(“tax”).value);
//var total=parseFloat(document.getElementById(“total”).value);
if(isNaN(numPrice)){
警报(“对不起,您必须输入一个数值才能下单”);
if(伊斯南(taxP))
警报(“对不起,您必须输入一个数值才能继续”);
}
var税=(numPrice*taxP)/100;
var总计=单价+税费;
document.getElementById(“total”).value=“$”+total.toFixed(2);
}
物价计算器
价格:
税款:
总数:
我想你忘了关闭{
和}
,你应该返回
以避免计算
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;
}
试试这个:
<!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:
<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>
物价计算器
函数isNumber(值){
返回值的类型==='number'&&
isFinite(值);
}
函数fixOrder()
{
document.getElementById(“total”).value=”“;//清除total字段
var numPrice=parseFloat(document.getElementById(“成本”).value);
var taxP=parseFloat(document.getElementById(“tax”).value);
//var total=parseFloat(document.getElementById(“total”).value);
if(isNaN(numPrice)){
警报(“对不起,您必须输入一个数值才能下单”);
return;//退出函数以避免计算
}
if(伊斯南(taxP)){
警报(“对不起,您必须输入一个数值才能继续”);
return;//退出函数以避免计算
}
var税=(numPrice*taxP)/100;
var总计=单价+税费;
document.getElementById(“total”).value=“$”+total.toFixed(2);
}
物价计算器
价格:
税款:
总数:
disabled=“disabled();”可能是个问题-将其设置为只读,并将其设置为税务
你可以从价格中算出税金和总额
第一次更改输入时,代码正在读取空值(NaN)
您不需要名称,也不需要为表单元素提供与它们自己的处理程序中的变量相同的标识符-它们在某些浏览器中是全局变量。请阅读javascript调试器。查看Firefox的Firebug或Chrome中的调试工具。IE9也有不错的开发工具。这使得解决这样的问题变得容易多了。下面是一个关于Chrome调试器的小教程。只要我在添加提示,就可以查看jsLint。它检测代码中的语法错误。例如,访问此小提琴并单击顶部的jsLint按钮。它会抓住这个问题。我剪切并粘贴到W3Tryit编辑器中,效果很好。不过,我认为我同意你需要学习调试工具。你需要在你认为是答案的帖子旁边打一个绿色的复选标记。在Chrome浏览器上按F12键打开控制台,在那里你可以输入javascript表达式进行测试。不使用isNumber()函数。但是你可以用它来测试一个值将来是否是一个数字。谢谢。看,我学到了课本上没有的东西。我修的这门课是在线的。所以我鼓励大家上网寻找答案。我得到了。disabled=“disabled”就可以了。教授给我的指导是使用disabled=“disabled”代码,现在代码运行得好多了。在第一个字段中输入一个值后,我在tax字段中输入了一个值,在total字段中得到了结果。伙计,我很高兴。