未捕获的TypeError:无法调用未定义的方法'Runme'

时间:2012-09-30 22:08:23

标签: javascript

我一直在检查这个问题,但我仍然无法弄清楚什么是错的。它应该取得我的成绩,并吐出我的津贴。之前它正在工作,但JSfiddle搞砸了,现在它还有其他问题。任何人都可以说出错了吗? 代码:

<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>

2 个答案:

答案 0 :(得分:1)

嗯,首先你需要一个<form>元素,这个元素显然已被注释掉了。

其他说明:
确保您始终使用相同的案例。 &#34;欠款&#34; !=&#34;欠&#34;。
onClick应该是&#34; onclick&#34;,尽管大多数浏览器都可以使用它。

<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>

答案 1 :(得分:0)

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

会失败,因为没有这样的表格,无论如何都不会有Runme方法。

document.personal...

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

尝试删除对伪造表单的引用,并替换

的所有用法
document.personal.inputName

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