在jqueryajax成功处理程序中访问它

在jqueryajax成功处理程序中访问它,jquery,ajax,Jquery,Ajax,在成功处理程序中,我试图将单击元素的html从保存更改为编辑。如果我把它直接放在.save_cat的单击处理程序下面,它就可以正常工作。一旦请求成功,我怎么做呢?您需要在live处理程序的本地变量中捕获它的值 //Save new category information $('.save_cat').live('click', function() { cat_id = $(this).attr("id").slice(4); cat_name = $(th

在成功处理程序中,我试图将单击元素的html从保存更改为编辑。如果我把它直接放在.save_cat的单击处理程序下面,它就可以正常工作。一旦请求成功,我怎么做呢?

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

  //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'
      });
  });

FWIW,在jQuery<1.7和jQuery 1.7+]中的
.live

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

  //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'
      });
  });

FWIW,在jQuery<1.7和jQuery 1.7+]中的
.live

谢谢,这就是我要找的:)live处理程序的第一行应该是
var元素=$(这个)
然后在
success
处理程序上操作变量
element
。谢谢,这就是我要找的:)活动处理程序的第一行应该是
var element=$(这)
然后在
success
处理程序上操作变量
元素