How can I rebind jquery events to links in a partial view with jquery .live()
Good morning everyone, I am having some trouble with rebinding jquery click events to some links that I have in a grid. The grid is in a partial view that gets refreshed using the jquery.load() method开发者_开发技巧. What I have done is tried to use .live() method to bind the click events to the links but it only works the first time the page is loaded once i click on one of the links and the grid is refreshed the jquery click events no longer fire. Here is an example of one of the click events and how I have written it.
$('.set-default-link').live('click', function () {
setDefaultLinkObj = $(this);
$('#dlg-phone-set-default').dialog('open');
return false;
});
The point of .live() is to bind events at the root of your DOM so that no matter what happens underneath, events generated by elements that match your selector will always trigger their handlers. So, it sounds to me like you're destroying the root of the DOM somehow in your partial reload. If not, you should try to replicate it in jsfiddle and I can help you out.
Also, see below for a better way to prevent the "click" from triggering default behavior.
$('.set-default-link').live('click', function (event) {
setDefaultLinkObj = $(this);
$('#dlg-phone-set-default').dialog('open');
// use this to prevent the click default from happening (cleaner and allows bubbling)
event.preventDefault();
});
精彩评论