2012-09-30 41 views
-4

我一直在检查这个问题一段时间,但我仍然无法弄清楚什么是错的。这应该是我的成绩,并吐出我的津贴。它之前在工作,但JSfiddle搞砸了,现在它有其他问题。谁能说出有什么问题? 代码:Uncaught TypeError:无法调用未定义的方法'Runme'

<head> 
<script> 
alert('loaded'); 
function Runme() { 
    alert("running"); 
    document.forms["form"].Runme(); 
    var Mathematics = document.personal.Mathematics; 
    var OMathematics = document.personal.OMathematics; 
    var Sci = document.personal.Sci; 
    var OSci = document.personal.OSci; 
    var ELA = document.personal.ELA; 
    var OELA = document.personal.OELA; 
    var SS = document.personal.SS; 
    var OSS = document.personal.OSS; 
    var Elec1 = document.personal.Elec1; 
    var OElec1 = document.personal.OElec1; 
    var Elec2 = document.personal.Elec2; 
    var OElec2 = document.personal.OElec2; 
    var Elec3 = document.personal.Elec3; 
    var OElec3 = document.personal.OElec3; 
    var Elem1 = document.personal.Elec1; 
    var OElem1 = document.personal.OElec1; 
    var Elem2 = document.personal.Elec2; 
    var OElem2 = document.personal.OElec2; 
    var Elem3 = document.personal.Elec3; 
    var OElem3 = document.personal.OElec3; 
    var Owed = 0; 
    var Calc = function(n, o) { 
     if (n >= 90) { 
      Owed = Owed + 1; 
      if (n >= 95) { 
       Owed = Owed + 1; 
      } 
     } 
     else if (owed >= 80) { 
      Owed = Owed + 0.5; 
     } 
     if (n > o) { 
      Owed = Owed + 0.5; 
     } 
     if (n < o) { 
      if (n > 95) { 
       Owed = Owed - 0.25; 
      } 
     } 
    }; 
    Calc(Mathematics, OMathematics); 
    Calc(Sci, OSci); 
    Calc(ELA, OELA); 
    Calc(SS, OSS); 
    Calc(Elec1, Elec1); 
    Calc(Elec2, Elec2); 
    Calc(Elec3, Elec3); 
    Calc(Elem1, Elem1); 
    Calc(Elem2, Elem2); 
    Calc(Elem3, Elem3); 
    alert(Owed); 
}; 
</script> 
</head> 
<body> 
<center> 
<h1>Grade Calculator</h1> 
<!--form name="form" action="grades.pl" onsubmit="return checkscript()"--> 
<h5>Math</h5><input type=text size=20 name=Mathematics> 
<h5>Old Math</h5><input type=text size=20 name=OMathematics> 
<h5>Science</h5><input type=text size=20 name=Sci> 
<h5>Old Science</h5><input type=text size=20 name=OSci> 
<h5>English</h5><input type=text size=20 name=ELA> 
<h5>Old English</h5><input type=text size=20 name=OELA> 
<h5>Social Studies</h5><input type=text size=20 name=SS> 
<h5>Old Social Studies</h5><input type=text size=20 name=OSS> 
<h5>Elective One</h5><input type=text size=20 name=Elec1> 
<h5>Old Elective One</h5><input type=text size=20 name=OElec1> 
<h5>Elective Two</h5><input type=text size=20 name=Elec2> 
<h5>Old Elective Two</h5><input type=text size=20 name=OElec2> 
<h5>Elective Three</h5><input type=text size=20 name=Elec3> 
<h5>Old Elective Three</h5><input type=text size=20 name=OElec3> 
<h5>Elem One</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem One</h5><input type=text size=20 name=OElec3> 
<h5>Elem Two</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem Two</h5><input type=text size=20 name=OElec3> 
<h5>Elem Three</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem Three</h5><input type=text size=20 name=OElec3> 
</form> 
<br/> 
<input type="button" value="Submit and Calculate" onClick="Runme()"/> 
</center> 

</body> 
+0

错?告诉我们。语法错误,错误的结果。什么? –

+0

@Valamas当前的是:Uncaught TypeError:不能调用未定义的方法'Runme' –

回答

0
document.forms["form"].Runme(); 

将失败,因为没有这样的形式,它不会有一个Runme方法反正。

document.personal... 

会失败,因为没有这样的事情。

尽量摆脱参考虚假形式,并与

document.getElementsByName("inputName")[0].value 
+0

它应该是什么? –

+0

@AidenEdwards,你能否更具体地说明我发现缺乏答案的哪一部分?你问什么是错的,我回答说。 –

1

好更换的

document.personal.inputName 

所有用途,首先你需要一个<form>元素,这显然是评论说。

其他说明:
请确保您始终使用相同的大小写。 “欠”!=“欠”。
onClick应该是“onclick”,尽管大多数浏览器都让它工作。

<head> 
<script> 
    //alert('loaded'); 
function Runme() { 
    // alert("running"); 
    //document.forms["personal"].Runme(); 
    var Mathematics = document.personal.Mathematics; 
    var OMathematics = document.personal.OMathematics; 
    var Sci = document.personal.Sci; 
    var OSci = document.personal.OSci; 
    var ELA = document.personal.ELA; 
    var OELA = document.personal.OELA; 
    var SS = document.personal.SS; 
    var OSS = document.personal.OSS; 
    var Elec1 = document.personal.Elec1; 
    var OElec1 = document.personal.OElec1; 
    var Elec2 = document.personal.Elec2; 
    var OElec2 = document.personal.OElec2; 
    var Elec3 = document.personal.Elec3; 
    var OElec3 = document.personal.OElec3; 
    var Elem1 = document.personal.Elec1; 
    var OElem1 = document.personal.OElec1; 
    var Elem2 = document.personal.Elec2; 
    var OElem2 = document.personal.OElec2; 
    var Elem3 = document.personal.Elec3; 
    var OElem3 = document.personal.OElec3; 
    var Owed = 0; 
    var Calc = function(n, o) { 
     if (n >= 90) { 
      Owed = Owed + 1; 
      if (n >= 95) { 
       Owed = Owed + 1; 
      } 
     } 
     else if (Owed >= 80) { 
      Owed = Owed + 0.5; 
     } 
     if (n > o) { 
      Owed = Owed + 0.5; 
     } 
     if (n < o) { 
      if (n > 95) { 
       Owed = Owed - 0.25; 
      } 
     } 
    }; 
    Calc(Mathematics, OMathematics); 
    Calc(Sci, OSci); 
    Calc(ELA, OELA); 
    Calc(SS, OSS); 
    Calc(Elec1, Elec1); 
    Calc(Elec2, Elec2); 
    Calc(Elec3, Elec3); 
    Calc(Elem1, Elem1); 
    Calc(Elem2, Elem2); 
    Calc(Elem3, Elem3); 
    alert(Owed); 
}; 
</script> 
</head> 
<body> 
<center> 
<h1>Grade Calculator</h1> 
<form name="personal" action="grades.pl"> 
<h5>Math</h5><input type=text size=20 name=Mathematics> 
<h5>Old Math</h5><input type=text size=20 name=OMathematics> 
<h5>Science</h5><input type=text size=20 name=Sci> 
<h5>Old Science</h5><input type=text size=20 name=OSci> 
<h5>English</h5><input type=text size=20 name=ELA> 
<h5>Old English</h5><input type=text size=20 name=OELA> 
<h5>Social Studies</h5><input type=text size=20 name=SS> 
<h5>Old Social Studies</h5><input type=text size=20 name=OSS> 
<h5>Elective One</h5><input type=text size=20 name=Elec1> 
<h5>Old Elective One</h5><input type=text size=20 name=OElec1> 
<h5>Elective Two</h5><input type=text size=20 name=Elec2> 
<h5>Old Elective Two</h5><input type=text size=20 name=OElec2> 
<h5>Elective Three</h5><input type=text size=20 name=Elec3> 
<h5>Old Elective Three</h5><input type=text size=20 name=OElec3> 
<h5>Elem One</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem One</h5><input type=text size=20 name=OElec3> 
<h5>Elem Two</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem Two</h5><input type=text size=20 name=OElec3> 
<h5>Elem Three</h5><input type=text size=20 name=Elec3> 
<h5>Old Elem Three</h5><input type=text size=20 name=OElec3> 
</form> 
<br/> 
<input type="button" value="Submit and Calculate" onclick="Runme()"/> 
</center> 

</body> 
+0

现在“Calc”功能不起作用。 :( –

+0

@AidanEdwards这是因为你需要传递对DOM元素的引用,当你需要获取它们的值(并比较它们)时。 – Ian

相关问题