Html javascript 错误:函数未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18561230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:54:46  来源:igfitidea点击:

javascript error: function is not defined

javascripthtml

提问by MrPaulius

I was trying to find the reason why my code doesnt work, and chrome comes back with this eror: Uncaught ReferenceError: calcMPG is not defined... Can someone spot my mistake ?(desperate)

我试图找出我的代码不起作用的原因,而 chrome 又出现了这个错误:Uncaught ReferenceError: calcMPG is not defined... 有人能发现我的错误吗?(绝望)

<script type="text/javascript">
    function calcMPG() {
        document.calc.startingMileage.value = startMileage;
        document.calc.endingMileage.value = endMileage;
        document.calc.gallonsUsed.value = gallUsed;
        var MPG = (endMileage - startMileage) / gallUsed;
        if (isNAN(startMileage) || isNAN(endMileage) || isNAN(gallUsed)) alert('please type in numbers only!');
        else document.calc.milesPerGalon.value = MPG;
    }
</script>

<form name="calc">Starting mileage:
    <input type="text" value="0" name="startingMileage" onchange="calcMPG()">
    <br>Ending mileage:
    <input type="text" value="0" name="endingMileage" onchange="calcMPG()">
    <br>Gallons used:
    <input type="text" value="0" name="gallonsUsed" onchange="calcMPG()">
    <br>Miles per galon:
    <input type="text" value="0" name="milesPerGalon">
</form>

回答by Satyam Koyani

Your declarations are wrong please correct them.

您的声明有误,请更正。

var startMileage = document.calc.startingMileage.value ;
var endMileage =  document.calc.endingMileage.value;
var gallUsed =  document.calc.gallonsUsed.value;

回答by Jonast92

startMileage

and your other right side references don't mean anything, they are undefined variables.

并且您的其他右侧引用没有任何意义,它们是未定义的变量。

Give your inputs an id and fetch the values from them like this:

为您的输入提供一个 id 并从它们中获取值,如下所示:

var startingMileage = document.getElementById('startingMileage').value;

回答by Marco Godínez

I don't know why you do that, but try this:

我不知道你为什么这样做,但试试这个:

<script type="text/javascript">
  function calcMPG(){
    var startMileage  = document.calc.startingMileage.value,
        endMileage    = document.calc.endingMileage.value,
        gallUsed      = document.calc.gallonsUsed.value,
        MPG           = (endMileage - startMileage) / gallUsed;

      if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed)){
        alert('please type in numbers only!');
      } else {
        document.calc.milesPerGalon.value = MPG;
      }
    }
</script>
<form name="calc">
    Starting mileage:<input type="text" value="0" name="startingMileage" onchange="calcMPG()"><br>
    Ending mileage:<input type="text" value="0" name="endingMileage" onchange="calcMPG()"><br>
    Gallons used:<input type="text" value="0" name="gallonsUsed" onchange="calcMPG()"><br>
    Miles per galon:<input type="text" value="0" name="milesPerGalon">
</form>

回答by Dmitry

Check here Fiddle

检查这里小提琴

 function calcMPG(){
        var startMileage = document.calc.startingMileage.value;
        var endMileage = document.calc.endingMileage.value;
        var gallUsed = document.calc.gallonsUsed.value;

        var MPG = (endMileage - startMileage) / gallUsed; 

        if(isNaN(startMileage) || isNaN(endMileage) || isNaN(gallUsed))
            alert('please type in numbers only!');
        else 
            document.calc.milesPerGalon.value = MPG;
    }