How to bind to another event after ajax call in jquery
I'm creating a graph using flot javascript library. I have enabled clickable event and the div is binded to plotclick event. So, when a datapoint is clicked, an ajax call is made and the result is used to replace the current div. After this i need to bind the div to a new event.
I tried to call bind, but it is still bound to old callback. When i call unbind and bind, the new callback is not called.
var handleTeacherClick = function( event, pos, item ) {
if( typeof item != "undefined" && item ) 开发者_开发问答{
var user_id = jQuery( 'input[name="id' + item.datapoint[0] + '"]' ).val();
jQuery.ajax({
type: 'POST',
url: BASEPATH + 'index.php/ajax/home/latest',
data: { "user_id": user_id },
dataType: 'json',
success: function ( result ) {
jQuery.plot(jQuery('#stats_prog'),
result.progress_data, result.progress_options);
jQuery.plot(jQuery('#stats_perf'),
result.performance_data, result.performance_options);
jQuery('.stats_title').
html('<span class="stats_title">'+
' >> Chapter '+Math.ceil(item.datapoint[0])+'</span>');
jQuery('#stats_prog')./*unbind("plotclick").*/
bind('plotclick', statClickHandler );
jQuery('#stats_perf')./*unbind("plotclick"). */
bind( 'plotclick', statClickHandler );
},
});
}
}
In your case use live
instead of bind event handler
For the new elements, you should use either of live
or delegate
. In case you are using a plugin, you will to use livequery
instead.
Instead of just calling unbind('plotclick')
, explicitly including the function you want to unbind (using unbind('plotclick',functionVar)
):
jQuery('#stats_prog').unbind("plotclick", handleTeacherClick).
bind('plotclick', statClickHandler );
精彩评论