我一直坚持通过 Jquery 添加和删除引导验证器类。如果 div 可见,我将添加验证,如果 div 隐藏,则删除。这是我的尝试:
<!--hidden form field-->
<div class="form-group">
<a href="javascript:validateField();" class="theme-color accountFormToggleBtn display-block">click here to change your password</a>
<div class="accountFormToggle display-none" id="passwordForm">
<div class="col-md-5">
<label for="password">Password</label>
<input type='password' id="password" placeholder="Password" name='pass' class="form-control" value='' data-bv-excluded="false" required>
</div>
<div class="col-md-5 col-md-offset-1">
<label for="exampleInputEmail1">Confirm password</label>
<input type='password' id="password2" placeholder="Confirm password" name='password2' class="form-control" value='' data-bv-excluded="false" data-match="#password" required>
</div>
</div>
</div>
JS代码:
function validateField() {
if($('#passwordForm').is(':visible')) {
$("#password").attr('data-bv-excluded',true);
$("#password2").attr('data-bv-excluded',true);
} else {
$("#password").attr('data-bv-excluded',false);
$("#password2").attr('data-bv-excluded',false);
}
}
如果 div 可见或隐藏,则验证工作。但确认密码与密码字段的数据不匹配。
请帮助我如何根据 div 的可见性匹配密码并确认密码字段?