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

Thi*_*its 0 ajax jquery

  //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'
      });
  });
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*ton 6

您需要在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"); }
Run Code Online (Sandbox Code Playgroud)

FWIW,.delegate优选超过.livejQuery中<1.7,和.on优选在jQuery的1.7+] 2.