<分区>
我有一个简单的注册页面:
<html>
<head>
<title>Register</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
<form action="">
<div>
<label style="display: block; padding-left: 0.3em;">REGISTER</label>
</div>
<div class="emailInputDiv">
<label class="email">Email </label>
<input type="text" name="email" id="em">
</div>
<div class="passwordInputDiv">
<label class="password">Password</label>
<input type="password" name="password" id="pw">
</div>
<div class="passwordInputDiv2">
<label class="password">Confirm Password</label>
<input type="password" name="password" id="pw2">
</div>
<div style="margin-bottom: 0.5em">
<button type="submit" class="signInButton" onclick="check()">
<span>Sign In</span>
</button>
</div>
</form>
<script>
let pw = document.getElementById("pw").value;
let pw2 = document.getElementById("pw2").value;
(function () {
if(pw != pw2){
$('.passwordInputDiv').css("color", "red");
$('.passwordInputDiv > input').css("color", "red");
$('.passwordInputDiv2').css("color", "red");
$('.passwordInputDiv2 > input').css("color", "red");
}
})();
</script>
</body>
</html>
我想在密码输入更改且不匹配时立即将其字体颜色设置为红色。该自执行函数在页面加载时运行,但我希望它在每次输入更改时运行。
而且那些 pw 和 pw2 是空的,因为它在页面加载时执行。所以函数没有执行。
我该如何调整?