开发者

jQuery's focus() gets triggered again on a blur()

Say I have an input field "lastname", but I don开发者_开发问答't want people to insert something if the "firstname" field is still empty. Example: http://jsfiddle.net/GQyHp/

$('input[name=lastname]').focus(function() {
  if (!$('input[name=firstname]').val()) {
    alert('First enter your first name');
    $(this).blur();
  }
});

If I click the "lastname" field I get the popup, but after I click 'OK' it fires the same popup again. If I remove the blur() there's no second popup.

How can I prevent this thing from being triggered by the blur() function?


I think the problem is related to alert().

When the alert box is closed, the focus is given to the control which was last focused by the user. This is my observation. Remove alert from the code at http://jsfiddle.net/GQyHp/1/ which is created by patrick and you'll see it will work.

So, not using built-in alert, instead using a fancy box for example may be a solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜