按Enter键进入文本框后,防止模糊和键盘事件触发

问题描述:

后输入我希望只触发 keyup 事件但是 blur 先被解雇。使用输入

After pressing enter I would like that only the keyup event be fired but blur is fired first. How to cancel blur when keyup is fired with enter?

请不要建议将 blur keyup 绑定到一个 live()方法。

Please don't suggest to bind both blur and keyup into a single live() method.

$(".textbox").on("blur",function () { 
    alert("blur Event fired");
});

$(".textbox").on("keyup",function (event) { 
    if(event.keyCode == 13){ // Detect Enter
        alert("KeyUp fired after pressing Enter");
    }
});


我无法改变 class 属性所以我最终得到了,

I Couldn't alter class attribute so i'd ended up with,

$(".textbox").on("blur",function () { 
    if($(this).attr('flag')!='1') 
        alert("blur Event fired");
});

$(".textbox").on("keyup",function (event) { 
    $(this).attr('flag','1');
    if(event.keyCode == 13){ // Detect Enter
        alert("KeyUp fired after pressing Enter");
    }
    $(this).attr('flag','0');
});