2012-10-03 21 views
1

在下面的代碼中,我將如何在我的ajax成功回調函數中使用$(this).parents('#friendRequest').remove();,並使用$(this)來獲取第一行中click事件的選擇器?jquery如何在ajax調用後獲取點擊事件的索引

jQuery(".notyAcceptFriendRequest").on('click', function() { 
    notyId = jQuery(this).attr("notyIdOne"); // pull in the id of the current selected notification 
    userProfilesUserId = $('#requestAlert').attr('userProfilesUserId'); // pull in the id of the current selected user profile id 
    if (notyId == userProfilesUserId) { // if notification id is the same as the user profiles id, do the following 
     $("#requestAlert").replaceWith('<span class="font1">You two are now friends</span>'); // the user profile friend requester will be replaced with the text 
     $(this).parents('#friendRequest').remove(); // find the '#friendRequest' id from the parent elements and fade it out 
    } else if (notyId != userProfilesUserId) { 
     /* the below will execute when the user 
     * clicks the 'Add' button and he/she is 
     * not on the targeted users page 
     */ 
     $.ajax({ 
      type: "POST", 
      dataType: "JSON", 
      url: "<?=base_url()?>index.php/reguserDash/acceptFriendNoty", 
      data: {notyId: notyId}, 
      json: {friendshipCaneled: true}, 
      success: function(data) { 
       if(data.acceptNotyFriendSuccess == true) { 
        $(this).parents('#friendRequest').remove(); 
       } 
      } 
     }) 
    } 
}); 

回答

5

回調this是不一樣的,在處理this

您需要將this存儲在局部變量中,然後在回調中使用該變量。

+0

哦很酷。謝謝,你解決了我的問題! –

+0

雖然答案在上下文中是正確的。你不應該使用那個選擇器。只需使用:$('#friendRequest')。remove()在可能的情況下,請始終按ID選擇。 – jholloman

相關問題