Do I need to unbind jquery event before remove element?

前端 未结 3 797
北荒
北荒 2020-12-24 10:37

I have a page using jquery-ui-dialog. Each time the dialog opens, page contents load in using ajax. Then it binds some event using jquery \"on()\". When the dialog close,

3条回答
  •  长情又很酷
    2020-12-24 11:29

    Oscar's answer is incomplete, if inside your partial (view that's loaded via ajax) you attached events using .on(), then you must call .off() before .empty().

    Look in the following code, if .off() is not called, events assigned in p1.html via standard .click() handler are removed when calling .empty(), but events assigned in p2.html via .on() are not removed and re assigned each time the partial is loaded.

    index.html

    
    
    
        

    Load Partial 1 | Load Partial 2

    p1.html

    This is the partial 1


    Link 1

    Link 2

    Link 3

    p2.html

    This is the partial 2


    Link 1

    Link 2

提交回复
热议问题