1

そのレコードを削除した後に div を非表示にする jQuery スクリプトがあります。ここにjQueryがあります。

$(document).ready(function () {
    $(".deleteComment").click(function ()
     {
        alert("asd");

        var Id = $(this).attr("id");

        var url1 = "@Html.Raw(Url.Action("DeleteComment", "Comment", new { id = "idValue" }))";
        url1=url1.replace("idValue",Id );
        alert(url1);

        $.ajax(
        {
            type: 'post',
            url: '/Comment/DeleteComment',
            dataType: 'json',
            data:
            { 
              'EId' : Id    
            },
            success: function (data) 
            {
                alert ("Hello");
                var commentBlock = $(this).closest('div');
                commentBlock.hide('slow');                                   
            }                
        });

問題は以下のコードのみです:

success: function (data) 
{
    alert ("Hello");
    var commentBlock = $(this).closest('div');
    commentBlock.hide('slow');
}

上記のコードをスクリプトの先頭に配置すると、正常に動作します。私が成功した場合、それは失敗します。

4

1 に答える 1

3

thisIgorが言及したようにajaxオブジェクトを参照し、彼が意味したのはこれです。

$(document).ready(function()) {
    var self = this;

    ...
    $.ajax(
        ...

        success: function(data) {
            $(self).closest("div").hide("slow");
        }
    );
}
于 2012-09-22T07:40:58.080 に答える