如果输入不正确,我正在使用 jQuery .focus()
使文本框接收焦点。但是当文本框获得焦点时,它不能失去它。我希望用户也能够选择其他文本框。我该怎么做?
function checkpassword() {
var password = $("#password").val();
var password2 = $("#password-reenter").val();
if(password != null){
if (password === password2) {
//alert("Passwords match");
$("#password-reenter").css('border','1px solid gray');
$("#password-warning").hide();
$("#password, #password-reenter").next('span').removeClass().addClass('ui-icon ui-icon-check');
} else {
$("#password-reenter").attr('title','The passwords do not match. Please reenter the password.').css('border','3px solid red').focus();
}
}
};
最佳答案
没有发现问题...
您可能还有其他代码触发焦点。
检查片段:
$("#password-reenter")
.attr('title','The passwords do not match. Please reenter thepassword.')
.css('border','3px solid red').focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="text" id="password-reenter" />
<input type="text" />
编辑:
在查看您的代码(在评论中给出)后,我发现了这一点:
<input type="password" name="password-reenter" id="password-reenter" onfocusout="checkpassword()" style="font-family: latine; border: 3px solid red;" required="" title="The passwords do not match. Please reenter the password.">
如您所见,onfocusout="checkpassword()"
导致了您的问题,因为它调用了 checkpassword()
,如果密码不匹配,它将再次聚焦.
关于javascript - jQuery 焦点不允许移出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29930032/