Javascript 如果isNaN,如何将变量的内容更改为零

Javascript 如果isNaN,如何将变量的内容更改为零,javascript,Javascript,可能重复: 我上周日交了作业。它被送回给我更正,因为isNaN值被返回到Total文本框。我想这就是编程要做的。相反,据她说,isNaN函数将检查变量并确定变量是否包含非数字数据,然后下一个语句将变量的内容更改为零,这让我感到困惑。这里的任何建议都是代码*回归;在我进行更改之前,语句不是语法错误。这里是上传文件的链接:ciswebs.smc.edu/cis54/tyson_schweidel/homework2.htm <!DOCTYPE HTML PUBLIC "-//W3C//DTD

可能重复:

我上周日交了作业。它被送回给我更正,因为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: &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>

当isNaN函数尝试检查变量时,变量不在范围内。

当isNaN函数尝试检查变量时,变量不在范围内。

您的格式错误,导致您在函数fixOrder真正执行任何操作之前退出该函数:

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");
        return; //gets syntax error
    }

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

    var tax = (numPrice * taxP) / 100;
    var total = numPrice + tax;
    numPrice = 0;
    taxP = 0;
    document.getElementById("total").value = "$" + total.toFixed(2);
    return; //gets syntax error
}
看看这有多容易阅读?您可以将原始代码缩减为:

document.getElementById("total").value = "$NaN";

您的格式设置混乱,导致您在函数fixOrder真正执行任何操作之前退出该函数:

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");
        return; //gets syntax error
    }

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

    var tax = (numPrice * taxP) / 100;
    var total = numPrice + tax;
    numPrice = 0;
    taxP = 0;
    document.getElementById("total").value = "$" + total.toFixed(2);
    return; //gets syntax error
}
看看这有多容易阅读?您可以将原始代码缩减为:

document.getElementById("total").value = "$NaN";

您需要将ifisNaNtaxP条件从嵌套if中去掉,否则它不会捕获错误,除非numPrice也不是数字。在返回之前,您还需要将字段设置为0。将字段设置为0是什么意思?什么字段,如return=0。您需要将ifisNaNtaxP条件从嵌套if中去掉,否则它不会捕获错误,除非numPrice也不是数字。在返回之前,您还需要将字段设置为0。将字段设置为0是什么意思?什么字段,比如return=0。