How can I check if a value is changed on blur event?

前端 未结 10 1208
时光说笑
时光说笑 2021-02-07 06:29

Basically I need to check if the value is changed in a textbox on the \'blur\' event so that if the value is not changed, I want to cancel the blur event.

If it possible

10条回答
  •  不知归路
    2021-02-07 06:47

    Why not just maintaining a custom flag on the input element?

    input.addEventListener('change', () => input.hasChanged = true);
    input.addEventListener('blur', () => 
    {
        if (!input.hasChanged) { return; }
        input.hasChanged = false;
        // Do your stuff
    });
    

    https://jsfiddle.net/d7yx63aj

提交回复
热议问题