我试图在输入时验证密码匹配,但是它不起作用有人可以告诉我错误

这是代码

<!DOCTYPE html>
<html>
<head>
<title>password change </title>
<script>
$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
});
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!");
    else
        $("#divCheckPasswordMatch").html("Passwords match.");
}
</script>
</head>
<body>
<div class="td">
    <input type="password" id="txtNewPassword" />
</div>
<div class="td">
    <input type="password" id="txtConfirmPassword"  />
</div>
    <div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
</body>
</html>

最佳答案

您必须包含对JQuery的引用才能使用它。

<!DOCTYPE html>
<html>
<head>
<title>password change </title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
});
function checkPasswordMatch() {
    var password = $("#txtNewPassword").val();
    var confirmPassword = $("#txtConfirmPassword").val();

    if (password != confirmPassword)
        $("#divCheckPasswordMatch").html("Passwords do not match!");
    else
        $("#divCheckPasswordMatch").html("Passwords match.");
}
</script>
</head>
<body>
<div class="td">
    <input type="password" id="txtNewPassword" />
</div>
<div class="td">
    <input type="password" id="txtConfirmPassword"  />
</div>
    <div class="registrationFormAlert" id="divCheckPasswordMatch">
</div>
</body>
</html>

09-20 16:20