到目前为止,我有这个代码可以帮助用户知道密码拼写是否正确,但我想让文本的颜色为绿色/红色以匹配/不匹配。
$(function() {
$("#txtConfirmPassword").keyup(function() {
var password = $("#txtNewPassword").val();
$("#divCheckPasswordMatch").html(password == $(this).val() ? "Passwords match." : "Passwords do NOT match!");
});
});
答案 0 :(得分:0)
使用.css()
更改文字颜色
$(function() {
$("#txtConfirmPassword").keyup(function() {
var password = $("#txtNewPassword").val();
if(password == $(this).val()) {
$("#divCheckPasswordMatch").html("Passwords match.");
$("#divCheckPasswordMatch").css("color","green");
} else {
$("#divCheckPasswordMatch").html("Passwords do NOT match!");
$("#divCheckPasswordMatch").css("color","red");
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="txtNewPassword" />
<input type="text" id="txtConfirmPassword" />
<div id="divCheckPasswordMatch" />
&#13;
答案 1 :(得分:0)
使用此:
$(function() {
$("#txtConfirmPassword").keyup(function() {
var password = $("#txtNewPassword").val();
$("#divCheckPasswordMatch").html(password == $(this).val() ? "Passwords match." : "Passwords do NOT match!").css('color','green');
});
});
答案 2 :(得分:0)
(function() {
$("#txtConfirmPassword").keyup(function() {
var password = $("#txtNewPassword").val();
if(password == $(this).val())
{
$("#divCheckPasswordMatch").html("Passwords match.");
$(this).css('color', 'green');
}else{
$("#divCheckPasswordMatch").html("Passwords do NOT match!");
$(this).css('color', 'red');
}
});
});
答案 3 :(得分:0)
设置确认密码
$("elmLocator").css("color","red");
但是在keyup上不要忘记改回来。