Accessing this in jquery ajax success handler

后端 未结 1 941
忘了有多久
忘了有多久 2021-01-27 10:32
  //Save new category information
  $(\'.save_cat\').live(\'click\', function() {

      cat_id =  $(this).attr(\"id\").slice(4);
      cat_name = $(this).parent().prev(         


        
相关标签:
1条回答
  • 2021-01-27 11:14

    You need to capture its value in a variable local to the live handler.

    $('.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 is preferred over .live in jQuery < 1.7, and .on is preferred in jQuery 1.7+]2.

    0 讨论(0)
提交回复
热议问题