最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to force blur without calling onBlur event - Stack Overflow

programmeradmin5浏览0评论

How do I force blurring, but without calling onBlur event in JavaScript/jQuery?

I'll try to describe you what I need it for:

  • when onBlur is called, I call a PHP script via jQuery and validating input. If there's something wrong, it returns a message and then I display it back in jQuery script.
  • if a blurred field isn't filled, script should focus user back to that field.

And the problem is that if you press TAB to change field, and your first field is not filled, script will focus you back, but then is called onBlur from second field that is also not filled, and then it causes an infinite loop.

So, i want to blur a field and focus to another without calling onBlur event.

How do I force blurring, but without calling onBlur event in JavaScript/jQuery?

I'll try to describe you what I need it for:

  • when onBlur is called, I call a PHP script via jQuery and validating input. If there's something wrong, it returns a message and then I display it back in jQuery script.
  • if a blurred field isn't filled, script should focus user back to that field.

And the problem is that if you press TAB to change field, and your first field is not filled, script will focus you back, but then is called onBlur from second field that is also not filled, and then it causes an infinite loop.

So, i want to blur a field and focus to another without calling onBlur event.

Share Improve this question edited Jun 25, 2012 at 15:15 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Jun 25, 2012 at 15:10 PixelPixel 1061 silver badge10 bronze badges 2
  • 1 Why not use change? And also run the validation on the submit event. – kapa Commented Jun 25, 2012 at 15:15
  • 2 Please don't force the focus back to the field the user is trying to leave, that is so annoying. When the user tries to submit it's OK to move the focus back to the problem field. – nnnnnn Commented Jun 25, 2012 at 15:16
Add a ment  | 

3 Answers 3

Reset to default 4

It's not an answer as spec'ed in your question, but what I'd suggest you do (in a somewhat UX sort of perspective) is to scrap the auto-refocus on invalid input, and instead mark the invalid field (may I suggest red, for example?). Now, you can go and deal with the (arguably) simpler problem of preventing a form submit on invalid data, instead of the problem of preventing a natural browser behavior type of an event.

Additionally, I'd dare to say that auto-refocusing is irritating for the user. Imagine tabbing to the second field, start typing, then suddenly yoink! You get dragged to the first field.

Why not just use on "change" and "keyup" instead of on blur?

I agree with Richard above, there's nothing more annoying than losing focus in the middle of typing, but if you do need to do this (Due to a customer requirement etc) - in the code that auto-focuses the field, disable the onBlur function temporarily until you've focused the field, then re-enable it.

发布评论

评论列表(0)

  1. 暂无评论