Input addEventListener on change not firing

前端 未结 2 1396
再見小時候
再見小時候 2021-01-26 05:59

I have a single input to upload a image.

html

相关标签:
2条回答
  • 2021-01-26 06:35

    change this.imageInput.addEventListener("change", this.uploadImage()); to this.imageInput.addEventListener("change", this.uploadImage); ( removed the () after this.uploadImage ).

    0 讨论(0)
  • 2021-01-26 06:50

    Change your event to this:

    this.imageInput.addEventListener("change", () => this.uploadImage());
    

    Or to this:

    this.imageInput.addEventListener("change", this.uploadImage.bind(this));
    

    What you are doing is calling uploadImage and passing the result of that to the listener. You want to pass the reference to the listener.

    0 讨论(0)
提交回复
热议问题