Why is my onchange function called twice when using .focus()?

前端 未结 6 2054
执念已碎
执念已碎 2021-01-20 09:32

TLDR

  • Check this example in chrome.
  • Type someting and press tab. see one new box appear
  • type something and pr
6条回答
  •  不思量自难忘°
    2021-01-20 09:56

    There's a way easier and more reasonable solution. As you expect onchange fire when the input value changes, you can simply explicitly check, if it was actually changed.

    function onChangeHandler(e){
      if(this.value==this.oldvalue)return; //not changed really
      this.oldvalue=this.value;
      // .... your stuff
    }
    

提交回复
热议问题