开发者

An alert placed on an input on jsfiddle.net repeats anywhere from 3 all the way up to 57 times. Why?

开发者 https://www.devze.com 2023-03-15 08:54 出处:网络
HTML: <input> JavaScript using jQuery: $(\'input\').focusin(function(){ alert(\'input focused\') });

HTML:

<input>

JavaScript using jQuery:

$('input').focusin(function(){
    alert('input focused')
});

Live copy on jsFiddle.net

Very strange behavior: when using jQuery to make a simple alert when an <input> or <textarea> gains focus, it causes the alert to repeat anywhere from 3 times all the way up to repeating 57 times. It's random, and that's without changing any code, sometimes it alerts 14 or 26 or 35 etc.

Does anyon开发者_如何学Pythone know why this happens? Am I missing something?

Edit: just realized this is happening in Chrome, but firefox 4 isn't repeating the alert.


The input probably loses it's focus when the alert pops up, and regains it when it's closed, causing it to fire again.

Try using console.log("input focus"); or any other means to see the event fire without making use of an alert and see if the problem still occurs.


The code is hooking the focusin event and then doing something to take focus away from the field temporarily (by taking it away from the window). When you dismiss the alert, the focus transfers back to the window and the field... Rinse, repeat.


It does work normally for me, it alerts just once ...

try

console.log('input fired');

instead of alert

0

精彩评论

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