button click event lost due to the alert box in text box onblur event

后端 未结 3 934
囚心锁ツ
囚心锁ツ 2021-01-18 07:36

I have created a simple web form, containing one text box and one button. I have captured the onblur event of the text box.



        
3条回答
  •  感动是毒
    2021-01-18 08:18

    A "click" of a button has two parts, mouse down and mouse up. When you mouse down on the button, it gains focus - blurring the text box and firing your alert. Since alert dialogs are modal, they halt all activity on the page so the button doesn't detect the mouse up and your click doesn't complete.

    It could be possible to work around your issue using a timer within the blur event, and cancelling that timer within the mousedown event of the button:

    var timer;
    function onTextBoxBlur()  
    {  
        timer = window.setTimeout(function () { alert("On blur"); }, 0);  
        return true;  
    }  
    
    function onButtonMouseDown()
    {
        clearTimeout(timer);
    }
    

提交回复
热议问题