0

所以我的代码基本上是告诉用户他们的注册出了什么问题,一切正常,但只有一件事。Div 应该更新文本字段以说明他们的注册有什么问题,但事实并非如此,这是我的 JavaScript

window.onload = initPage;
submitBtn = document.getElementById("submit");
errorText = document.getElementById("errortext");


function initPage() {
    document.getElementById("password2").onkeyup = checkPassword;
    document.getElementById("password2").onblur = checkPassword;
    document.getElementById("email2").onkeyup = checkEmail;
    document.getElementById("email2").onblur = checkEmail;
    submitBtn.disabled = false;
}

function checkPassword() {
    var password1 = document.getElementById("password1").value;
    var password2 = document.getElementById("password2").value;
    if (password1 != password2) {
        document.getElementById("password1").className = "denied";
        document.getElementById("password2").className = "denied";
        submitBtn.disabled = true;
        document.getElementById("submit").className = "denied";
        errorText.innerHTML = "Passwords do not match.";
    } else {
        document.getElementById("password1").className = "password2";
        document.getElementById("password2").className = "password2";
        document.getElementById("submit").className = "button1";
        submitBtn.disabled = false;
        errorText.innerHTML = "";
    }
}

function checkEmail() {
    var email1 = document.getElementById("email1").value;
    var email2 = document.getElementById("email2").value;
    if (email1 != email2) {
        document.getElementById("email1").className = "denied";
        document.getElementById("email2").className = "denied";
        submitBtn.disabled = true;
        document.getElementById("submit").className = "denied";
        errorText.innerHTML = "Emails Do Not Match.";

    } else {
        document.getElementById("email1").className = "regtext";
        document.getElementById("email2").className =  "regtext";
        submitBtn.getElementById("submit").disabled = true;
        document.getElementById("submit").className = "button1";
        errorText.innerHTML = "";
    }
}

这是我的 div

<tr><td colspan="2"><div id="errortext"></div></td></tr>
4

2 回答 2

1

您可能正在尝试在 DOM 完全构建之前访问该 div。将该代码与initPage函数一起放入。您需要该函数范围之外的变量,以便您的其他函数可以访问它们。您应该将所有这些都放在一个闭包中以避免创建全局变量。

前任:

(function(){    
    var submitBtn;
    var errorText;

    function initPage() {
        submitBtn = document.getElementById("submit");
        errorText = document.getElementById("errortext");
        document.getElementById("password2").onkeyup = checkPassword;
        document.getElementById("password2").onblur = checkPassword;
        document.getElementById("email2").onkeyup = checkEmail;
        document.getElementById("email2").onblur = checkEmail;
        submitBtn.disabled = false;
    }

    // This next line should be under the declaration above
    window.onload = initPage;

    // ...
})();
于 2013-02-01T02:01:45.367 回答
0

您必须通过 id 获取元素,然后设置其内部 html。

document.getElementById("errorText").innerHTML = "Emails Do Not Match.";

在你直接使用 errorText.innerHTML 的地方做同样的事情。

于 2013-02-01T02:00:42.970 回答