JQuery的问题....重点不工作 [英] JQuery Problem .... Focus not working

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

问题描述

这里是我的jquery的代码...

我想验证一个文本字段的数值。如果无效,那么再次集中相同的文本字段和agian ..

  $('#price')。blur(function(){ $(this).val()!='Preçoem USD'&&&&; isNaN($(this).val()))
{
alert(Enter整数只...);
$('#price')。focus();
}
});

我想设置#price焦点,如果它不是一个整数...我已经使用$(this )等也仍然不工作

在此先感谢...

解决方案

这里的问题似乎与模糊事件完成和焦点事件触发之间的时间间隔。

将焦点包裹在setTimeout中允许模糊事件完成,然后将焦点置于($($))。

$ $ $ $ $ $' .val()!='Preçoem USD'&& isNaN($(this).val()))
{
alert(Enter Integer only ...);
$ b setTimeout(function(){
$('#price')。focus();
},100);
}
});

您可以在这里看到这个工作副本: http://jsfiddle.net/ttQRD/
$ b

更新 p>

正如在评论中已经指出的那样,这里的问题似乎与时间有关,而更多的是与焦点事件的委派。

将焦点事件放在 setTimeout 中,可以完成当前的模糊事件,并触发焦点事件一个单独的循环。

here is my code of jquery ...

i want to validate a text field for numeric value. if invalid then focus same text field again and agian ..

$('#price').blur(function(){
    if($(this).val() !='Preço em USD' && isNaN($(this).val()))
    {
        alert("Enter Integer only ...");
        $('#price').focus();
    }
});

i want to set #price focus if its not an integer... i have used $(this) etc also but still not working
Thanks in Advance ...

解决方案

The issue here seems to be with the amount of time between the blur event completing and the focus event triggering.

Wrapping the focus in a setTimeout allows the blur event to complete before setting the focus on the element.

$('#price').blur(function(){
    if($(this).val() !='Preço em USD' && isNaN($(this).val()))
    {
        alert("Enter Integer only ...");

        setTimeout(function(){
        $('#price').focus();
        },100);
    }
});

You can see a working copy of this here: http://jsfiddle.net/ttQRD/

UPDATE

As has been pointed out below in the comments, the issue here seems less to do with time and more to do with the delegation of the focus event.

Putting the focus event inside the setTimeout allows the current blur event to complete and the focus event to fire in a seperate loop.

这篇关于JQuery的问题....重点不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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