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

后端 未结 3 683
梦毁少年i
梦毁少年i 2020-12-12 03:32

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\

相关标签:
3条回答
  • 2020-12-12 04:09

    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>
    
    0 讨论(0)
  • disabled="disabled();" may be a problem- make it read only, and the tax as well.

    You can figure the tax and the total from the price.

    Your code is reading an empty value (NaN) the first time you change either input.

    You don't need names and don't give form elements the same identifiers as the variables in their own handlers- they are globals in some browsers..

    0 讨论(0)
  • 2020-12-12 04:15

    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;
    }
    
    0 讨论(0)
提交回复
热议问题