Do I need to clean up or remove event handlers after ajax load of grid & form content
After a click on a d/b table name, my 'admin' plugin loads a grid of table record rows, a jQuery-UI modal dialog input form and an OK To Delete modal form for that table. Event handlers are set for the grid row modify/delete buttons, column sort buttons, the optional pagination object. The form event handlers include setting the form values, validation and ajax submission.
Currently, the Ajax replaces the inner html of the 'content' div below
<div id='content'>
<div id='deletemsg'></div>
<div id='inputform'></div>
<div id='grid'></div>
</div>
My question is what, if anything, do I have to do to remove/destroy the event code for the prev开发者_JS百科ious table data that has been replaced ?
To remove the old events, you can use unbind()
.
Use "live" method to set the event handlers. This way it'll affect the mark-up it finds, whether it's original table or replaced table, so, you don't have to worry about destroying/re-attaching events.
Update:
Going jQuery 1.7+, use on()
instead of live()
.
Details: http://www.elijahmanor.com/2012/02/differences-between-jquery-bind-vs-live.html
精彩评论