document.getElementById出错:未捕获TypeError:undefined不是函数

时间:2014-08-10 17:01:05

标签: javascript html dom syntax-error

我收到以下错误 " Uncaught TypeError:undefined不是函数" 以及我尝试解决问题的任何内容。我参加了一个在线课程,这是我最后一个项目的一部分,但我没有得到任何解决方案,请解释我如何解决这个以及为什么这会发生在我的代码中,谢谢,这是我的代码。

<html>
<head>
<title>My form</title>
</head>
<script type="text/JavaScript">
function register() {
    var x = new Array();
    x[0] = document.getElementById('name').value;
    x[1] = document.getElementById('lname').value;
    x[2] = document.getElementById('email').value;
    x[3] = document.getElementById('password').value;
    x[4] = document.getElementById('cpassword').value;

    var h = new Array();
    h[0] = "<span style='color:red'>Please type your name!</span>";
    h[1] = "<span style='color:red'>Please type your last name!</span>";
    h[2] = "<span style='color:red'>Please type your email!</span>";
    h[3] = "<span style='color:red'>Please type your password!</span>";
    h[4] = "<span style='color:red'>Please confirm your password</span>";

    var divs = new Array("mname", "mlname", "memail", "mpassword", "mcpassword");

    for (i in x) {

        var error = h[i];
        var div = divs[i];

        if (x[i]=="") {
            document.getElementById(div).innerHTML = error;
        }else{
            document.getElementById(div).innerHTML = "OK!";
        }
    }
}

function pass() {

    var first = document.getElementById('password').value;
    var second = document.getElemntById('cpassword').value;

    if (second == first) {
        document.getElementById('mcpassword').innerHTML = "OK!";
    } else {
        document.getElementById('mcpassword').innerHTML = "<span style='color: red'>Your password don't match!</span>";
    }
}
</script>
<body>
    <form>
        <table border="1">
                <tr><td>Name:</td><td><input type="text" name="name" id="name" onkeyup="register()"/></td><td><div id="mname"></div></td></tr>
                <tr><td>Last Name:</td><td><input type="text" name="lname" id="lname" onkeyup="register()"/></td><td><div id="mlname"></div></td></tr>
                <tr><td>Email:</td><td><input type="text" name= "email" id="email" onkeyup="register()"/></td><td><div id="memail"></div></td></tr>
                <tr><td>Password:</td><td><input type="password" name="password" id="password" onkeyup="register()"/></td><td><div id="mpassword"></div></td></tr>
                <tr><td>Confirm Password:</td><td><input type="password" name="cpassword" id="cpassword" onkeyup="pass()"/></td><td><div id="mcpassword"></div></td></tr>
        </table>
        <br />
    <input type="button" name="submit" value="submit"/>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这是错误

你在说

var second = document.getElemntById('cpassword').value;

应该是

var second = document.getElementById('cpassword').value;