在让用户继续操作之前,如何确保密码字段匹配?

<input name="pass" id="pass" type="password" />
<input type="password" name="cpass" id="cpass" /> <span id='message'></span>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$('#pass, #cpass').on('keyup', function () {
if ($('#pass').val() == $('#cpass').val()) {
    $('#message').html('Matching').css('color', 'green');
}
else $('#message').html('Not Matching').css('color', 'red');
});
 </script>
<input type="text" name="phone" id="phone">

最佳答案

您可以通过在电话字段的输入中添加属性disabled来启用基于条件的禁用文本框:

$('#pass, #cpass').on('keyup', function () {
  if ($('#pass').val() == $('#cpass').val()) {
   $('#message').html('Matching').css('color', 'green');
   $("#phone").removeAttr("disabled");
  }
 else {
   $('#message').html('Not Matching').css('color', 'red');
   $("#phone").attr("disabled", "disabled");
  }
 });


Working Demo

10-02 21:37