在jquery ajax成功处理程序中访问它

//Save new category information $('.save_cat').live('click', function() { cat_id = $(this).attr("id").slice(4); cat_name = $(this).parent().prev().prev().children('.cat_name_edit').val(); sort_order = $(this).parent().prev().children('.sort_order_edit').val(); $.ajax({ type: 'POST', url: '../file/category/update_category', data: { 'cat_id' : cat_id, 'sort_order' : sort_order, 'cat_name' : cat_name}, beforeSend:function(){ //action while loading }, success:function(data){ alert('hi'); $(this).html("Edit"); }, error:function(){ // failed request; give feedback to user alert("An unexpected error has occurred. Your category could not be saved."); }, dataType : 'json' }); }); 

在成功处理程序中,我正在尝试将单击元素的html从“保存”更改为“编辑”。 如果我将它直接放在.save_cat的单击处理程序下面,它可以正常工作。 请求成功后我该怎么办?

您需要在live处理程序的本地变量中捕获其值。

 $('.save_cat').live('click', function() { var $that = $(this); // These should be vars. var cat_id = $that.attr("id").slice(4); ... $.ajax({ ... success: function() { $that.html("Edit"); } 

FWIW, .delegate比jQuery <1.7中的.live .on ,而.on在jQuery 1.7+中更.on .2 。