Best way to remove an event handler in jQuery?

前端 未结 20 2331
庸人自扰
庸人自扰 2020-11-21 11:38

I have an input type=\"image\". This acts like the cell notes in Microsoft Excel. If someone enters a number into the text box that this input-image

20条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 11:40

    All the approaches described did not work for me because I was adding the click event with on() to the document where the element was created at run-time:

    $(document).on("click", ".button", function() {
        doSomething();
    });
    


    My workaround:

    As I could not unbind the ".button" class I just assigned another class to the button that had the same CSS styles. By doing so the live/on-event-handler ignored the click finally:

    // prevent another click on the button by assigning another class
    $(".button").attr("class","buttonOff");
    

    Hope that helps.

提交回复
热议问题