将字符串与document.getElementById()进行比较

Comparing strings with document.getElementById()

本文关键字:比较 getElementById 字符串 document      更新时间:2023-09-26

我正在尝试比较此函数中的密码字段。如果ID为txtPassword的第一个密码字段与ID为txtpaWVerified的第二个密码字段相同,则它应该写入消息"password matches!",否则它应该显示另一条消息。目前,它只显示"密码匹配!"消息,即使它们不匹配。我该怎么解决这个问题?非常感谢。

function verifyPassword(){
    document.getElementById("verifyPassword").innerHTML = "Second try needed. Please make sure passwords match.";
};
function goodPassword(){
    document.getElementById("verifyPassword").innerHTML = "Passwords match!";
};
$("#txtPWVerified").blur(function(){
    var pass1 = document.getElementById("txtPassword");
    var pass2 = document.getElementById("txtPWVerified");
    if (pass1.innerHTML == pass2.innerHTML) {
        goodPassword();
    } else {
        verifyPassword();
    };
});

编辑:下面是正确的代码,感谢您的帮助!

function verifyPassword(){
    document.getElementById("verifyPassword").innerHTML = "Second try needed. Please make sure passwords match.";
};
function goodPassword(){
    document.getElementById("verifyPassword").innerHTML = "Passwords match!";
};
$("#txtPWVerified").blur(function(){
    var pass1 = document.getElementById("txtPassword").value;
    var pass2 = document.getElementById("txtPWVerified").value;
    if (pass1 == pass2) {
        goodPassword();
    } else {
        verifyPassword();
    };
});

my hint;)

$("#txtPWVerified").blur(function(){
var pass1 = document.getElementById("txtPassword").value;
var pass2 = document.getElementById("txtPWVerified").value;
alert(pass1);
alert(pass2);