2012-09-30 40 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

相關問題