1

我有一个 jQuery 脚本,我将在删除该记录后隐藏 div。这是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

this将引用 Igor 提到的 ajax 对象,他的意思是这个。

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

    ...
    $.ajax(
        ...

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