繁体   English   中英

隐藏div在jQuery Ajax成功中无法正常工作

[英]Hiding a div is not working properly in jQuery Ajax success

我有一个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');
}

如果我将代码放在脚本的开头,那么它可以正常工作。 如果我成功了,那就失败了。

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

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

    ...
    $.ajax(
        ...

        success: function(data) {
            $(self).closest("div").hide("slow");
        }
    );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM