2013-02-01 67 views
0

所以我的代碼基本上是告訴用戶他們的註冊有什麼問題,一切正常,但一件事。股利應更新文本字段說有什麼錯他們的註冊,但它不是,這是我的JavaScript爲什麼我的div沒有正確更新?

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> 

回答

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; 

    // ... 
})(); 
+0

你間接地幫我解決這個問題,謝謝。 。還有一個問題,電子郵件是頁面上的最後兩個文本字段,所以你不能onblur,並且onkeyup不能正確更新它,該按鈕被禁用,並且當我完成打字時文本仍然出現,怎麼可能我可以在不模糊的情況下正確更新 – Gacnt

+0

不客氣!但我不知道你對其他問題的含義。你能解釋一下嗎? –

+0

好了,我的表單上的最後2個文本字段是電子郵件和確認電子郵件,只要我輸入確認電子郵件,它應該檢查,確認它沒問題,並啓用提交按鈕,以及清除在上面我的代碼中可以看到的文本,但由於某種原因它不會在keypressup上更新 – Gacnt

0

你要獲取由ID的元素然後設置其內部的html。

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

和以往任何時候都直接使用errorText.innerHTML地方做同樣的。

+0

我有這樣的第一,但它沒有工作,所以我想,也許做一個變量將修復它,但它還是不 – Gacnt

相關問題