I have a asp.net MVC partial view, inside it jQuery is used to popup a datepicker for a textbox. A cancel button using Ajax to reload the partial view when it is clicked. All work well in the first load. But after the cancel button is clicked, all the jQuery functionality disappear, for example the datepicker stop showing up for the textbox. Any idea what could be wrong with it?
2 Answers
If you're doing any updates to the partial view via ajax any events bound using jquery will need to be rebound or bind them using jquery live, or jquery on for 1.7+
$(document).on(events, selector, data, handler); // jQuery 1.7+
2 Comments
xiaobing
I put the events binding inside the partial view $(document).ready function. I assumed that runs every time the partial view updated via ajax, am I wrong?
NullReference
@user1552107 $(document).ready doesn't get executed after an ajax update. There are many different ways to fix this the the easiest is to use the jquery live binding so that after an ajax update you events aren't lost
When you reload your partial view any handlers on the elements in the old (now replaced) partial will have been lost. You have to rebind them to get the functionality back.
A script block at the end of your partial that binds the handlers would work. Or the code that handles the AJAX success or complete call back can rebind them if you prefer.
onevent handlers), when you reload the partial view, you're probably wiping out those listeners. Post some code so we see how you're setting everything up.