Javascript 如何使用JQuery多次更改禁用的标记
我正在尝试使用JQuery验证密码并确认密码对字段。我面临的问题是,如果我包括以下功能:Javascript 如何使用JQuery多次更改禁用的标记,javascript,jquery,html,Javascript,Jquery,Html,我正在尝试使用JQuery验证密码并确认密码对字段。我面临的问题是,如果我包括以下功能: <script> $('#password, #confirm_password').on('keyup', function () { if ($('#password').val() == $('#confirm_password').val()) { $('#message').html('Matching').css('color', 'g
<script>
$('#password, #confirm_password').on('keyup', function () {
if ($('#password').val() == $('#confirm_password').val()) {
$('#message').html('Matching').css('color', 'green');
$("#submit").prop('disabled', false);
} else
$('#message').html('Not Matching').css('color', 'red');
});
</script>
然后,无论密码是否匹配,提交按钮始终处于禁用状态
有人能解释一下我遗漏了什么以及如何纠正这个问题吗 将你的else设置为一个块
$('#password, #confirm_password').on('keyup', function () {
if ($('#password').val() == $('#confirm_password').val()) {
$('#message').html('Matching').css('color', 'green');
$("#submit").prop('disabled',false);
} else {
$('#message').html('Not Matching').css('color', 'red');
$("#submit").prop('disabled',true);
}
});
如果没有大括号,则只有消息由else执行,而disabled始终执行
我建议您始终使用大括号,即使您只想执行一条语句。使读取和维护代码更加清晰。使您的else成为一个块
$('#password, #confirm_password').on('keyup', function () {
if ($('#password').val() == $('#confirm_password').val()) {
$('#message').html('Matching').css('color', 'green');
$("#submit").prop('disabled',false);
} else {
$('#message').html('Not Matching').css('color', 'red');
$("#submit").prop('disabled',true);
}
});
如果没有大括号,则只有消息由else执行,而disabled始终执行
我建议您始终使用大括号,即使您只想执行一条语句。使阅读和维护代码更加清晰