使用document.getElementById()比较字符串

时间:2015-11-04 00:56:01

标签: javascript jquery

我正在尝试比较此功能中的密码字段。如果ID为txtPassword的第一个密码字段与ID为txtPWVerified的第二个密码字段相同,则应写入消息"密码匹配!"如果它不显示另一条消息。目前它只显示"密码匹配!"即使他们不匹配也会留言。我怎样才能解决这个问题?谢谢!!

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();
    };
});

1 个答案:

答案 0 :(得分:1)

我的提示;)

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