我正在尝试比较此函数中的密码字段。如果 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();
};
});
最佳答案
我的提示;)
$("#txtPWVerified").blur(function(){
var pass1 = document.getElementById("txtPassword").value;
var pass2 = document.getElementById("txtPWVerified").value;
alert(pass1);
alert(pass2);
关于javascript - 使用 document.getElementById() 比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511928/