jQuery焦点不起作用 [英] jQuery focus not working

查看:101
本文介绍了jQuery焦点不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力专注于信用卡验证失败。但奇怪的是 focus()无效。

I am trying to focus on credit card validation fail. but strange focus() not working.

$('#cc_number').validateCreditCard(function (result) {
    if (result.length_valid && result.luhn_valid) {

    } else {
       $("#cc_number").focus();
       return false;
    }
});

<input  type="text" id='cc_number' name="cc_number" />


推荐答案

作为在评论中提到,您原来的jsfiddle包含解决方案。

As mentioned in the comments, your original jsfiddle contains the solution.

解决方法是在焦点调用上设置超时。

A workaround is to put a timeout on the focus call.

setTimeout(function(){
    $("#cc_number").focus();
}, 0);

我不是100%肯定,但可能是因为模糊时调用警报,你从来没有真正允许文本框失去焦点,因此当调用焦点时,它已经拥有它。

I'm not 100% sure, but it could be that as the alert is called on blur, you're never actually allowing the textbox to lose focus, and therefore when focus is called, it already has it.

通过使用超时,你将逻辑放入一个单独的线程,它与主要的javascript代码分开运行(很像一个回调函数)。

By using a timeout, you are putting the logic into a seperate thread, which runs seperate to the main javascript code (much like a callback function).

但是作为你的问题评论还提到,强制关注直到验证通过是令人讨厌的。

But as your question comments also mention, forcing focus until validation passes is annoying.

但这更像是一种用户体验批评。

But this is more of a UX criticism.

这篇关于jQuery焦点不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆