2012-09-22 6 views
1

jQuery 스크립트가 있는데, 해당 레코드를 삭제 한 후에 div를 숨길 것입니다. 다음은 jQuery입니다.jQuery Ajax 성공시 div 숨기기가 제대로 작동하지 않습니다.

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

다음 그것을 잘 작동합니다. 성공하면 성공하지 못합니다.

+0

오류가 있습니까? – MikkoP

+0

No..It 그냥 일하지 ... 그 코드는 내가 처음부터 그때 그것은 완벽하게 workd에 배치 ... – user1668543

+0

"이"성공 콜백에서 "아약스 개체를 처리합니다. 따라서 "var self = this;"와 같은 선언을 한 다음 ajax 호출에서 $ (self) .closest 등을 사용해야합니다. –

답변

3

this은 Igor이 언급 한 ajax 객체를 가리키며 이것이 의미하는 바가됩니다.

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

    ... 
    $.ajax(
     ... 

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

많이 고마워 ...이게 효과가있어 ... – user1668543

+0

고마워요 @MikkoP, 정확히 무슨 뜻이야! –

관련 문제