简体   繁体   中英

OnBlur firing multiple times

I am using onblur for text box data checking. OnBlur is working fine until or unless we are not pressing ALT + TAB or windows button .

If I pressed ALT + TAB then blur function executing multiple times..

For example : https://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_event_blur_alert

Initially it will work fine then press alt+tab then it will execute multiple times.

Please suggest how to resolved this issue.

Please follow below steps to re produce the issue

  1. Click on the above link
  2. Focus on the text box and then press alt+tab

This is because you are prompting an alert on blur, when you press alt+tab it will focus on different window and when you click on alert box it will again focus to text field and will cause fire an event, This will do infinity. You should avoid alert here

 $(document).ready(function(){ $("input").blur(function(){ console.log("This input field has lost its focus."); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> Enter your name: <input type="text"> <p>Write something in the input field, and then click outside the field to lose focus (blur).</p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM