1

在下面的代码中,我将如何$(this).parents('#friendRequest').remove();在我的 ajax 成功回调函数中使用,并用于$(this)在第一行获取 click 事件的选择器?

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();
                }
            }
        })
    }
});
4

1 回答 1

5

this在回调中与this在处理程序中不同。

您需要存储this在局部变量中,然后在回调中使用该变量。

于 2012-10-03T02:11:50.660 回答