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

前端 未结 10 1211
时光说笑
时光说笑 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:55

    Something like this. Using Kevin Nadsady's above suggestion of

    this.value!=this.defaultValue

    I use a shared CSS class on a bunch of inputs then do:

        for (var i = 0; i < myInputs.length; i++) {
            myInputs[i].addEventListener('blur', function (evt) {
                if(this.value!=this.defaultValue){
                    //value was changed now do your thing
                }
            });
            myInputs[i].addEventListener('focus', function (evt) {
                evt.target.setAttribute("value",evt.target.value);
            });
        }
    

提交回复
热议问题