Prevent both blur and keyup events to fire after pressing enter in a textbox

后端 未结 8 423
粉色の甜心
粉色の甜心 2021-01-11 10:13

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

8条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-11 11:14

    I had a similar problem. I'm allowing the user to edit a single field and when they press Enter or move off that field, I send an Ajax request to update it. I found that when I pressed Enter the Ajax request was sent twice.

    So rather than calling my request on Enter, I tried calling $input.blur() instead, and that worked! Which got me to thinking... If the Enter key causes a blur do I even need to try to capture it?

    In my case, I did not need to. I found that all I needed was the blur because the Enter key triggered it anyway. I'm not sure if that's 100% across all browsers, but I did test on current versions of Chrome, Firefox, IE and Safari, all of which worked fine.

    So, I suggest that you either just don't capture the Enter at all, or you simply call blur() to trigger the actions you need.

提交回复
热议问题