为什么我的输出未定义或NaN?

时间:2017-07-06 12:22:17

标签: javascript

出于某种原因,当我输入值时,所有输出都是 undefined和NaN,我不明白为什么。我想 错误很简单。所以很简单,我会面对。抱歉 对于我的代码,我还是javascript的新手。而且,没关系信用卡。

function processAmount(){ 
    var numOfLiters= parseInt(document.estore.noOfLiters.value);
    var result;
    var perLiterAmount;
    var gasType;

    switch(gasType){
    case "Premium": 

            perLiterAmount= 50;
            result= 50*numOfLiters;
            break;
    case "Diesel":
           perLiterAmount= 38;
          result= 38*numOfLiters;
            break;

    case "Unleaded":
           perLiterAmount= 44;
            result= 44*numOfLiters;
            break;
    }



    var vat= result*0.12;

    document.write("<p>Fuel Type: " + gasType + "</p>");
    document.write("<p>Price per Liter amount: " + perLiterAmount + "</p>");
    document.write("<p>Purchase Amount: " + result + "</p>");
    document.write("<p>VAT: " + vat + "</p>");
    document.write("<p>TOTAL AMOUNT: " + (result+vat) + "</p>");
    document.write("<p>Credit Card : " + creditCard + "</p>");`

以下是表格:

 <form name='estore' id='estore'>
   <fieldset>
            <legend>Gasoline eStore</legend>
                <p>Enter gasoline type:
                    <select name='gastype' id='gastype'>
                        <option value="Premium"> Premium</option>
                        <option value="Unleaded"> Unleaded</option>
                        <option value="Diesel"> Diesel</option>
                        </select></p>

                <p> Enter number of Liters:
                    <input type='number' name='noOfLiters' id='noOfLiters'/>
                </p>

                <p> Enter Credit Card Number:
                    <input type="number" name='creditCardNumber' id='creditCardNumber'/>
                </p>

                <p>
                    <input type='button' value='Submit' 
                        onclick="processAmount()"/>&nbsp;&nbsp;&nbsp;
                    <input type='reset'/>
                </p>    

3 个答案:

答案 0 :(得分:2)

首先将值分配给gasType,然后将其放入switch大小写。

答案 1 :(得分:0)

你可以替换

var gasType;

var gasType = document.getElementById('gastype').value;

这样您的方法processAmount()应该按预期工作。 正如其他一些回复已经建议的那样,您没有初始化变量gasType,因此它是undefined

使用上面的行,您可以使用select字段中的值初始化它。

答案 2 :(得分:0)

在您的函数中,将值指定为load

securitypolicyviolation
相关问题