2011-04-13 24 views
1

이전 div 태그와 다음 .pclass를 삭제하는 데 사용되는 jquery 코드가 있습니다. Heres는 내 코드 :jquery는 요소를 제거하지 않습니다.

$(".delete").bind("click",function(){ 
      var c = confirm("You sure want to delete this?"); 
      if(c){ 
       /* $(this).next('.pclass').remove(); 
       $(this).prev('.rurl').remove(); 
       $(this).remove();*/ 
       var text = $(this).prev('.rurl').text(); 
       var idvalue = $(this).prev('.rurl').attr('id'); 
       var id = idvalue.split("|"); 

       $.ajax({ 
         type: "POST", 
         url: "http://localhost:8080/cPEP_UI/Engine_rurl_delete", 
         data: "text="+text+"&eid="+id[1], 
         dataType: "json", 
         success: function(data) { 
          if(data.update == "success"){ 
           $(this).next('.pclass').remove(); 
           $(this).prev('.rurl').remove(); 
           $(this).remove(); 
          } 
          // $('#show').show(); 
          //$('#show').html(data.update+" "+data.message).fadeOut(8000); 
         }, 
        error:function(xhr,err){ 
         //alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); 
         $('#show').show(); 
         // alert(xhr.responseText); 
         $('#show').html("responseText: "+xhr.responseText); 

        } 

        }); 
      } 
     }); 

나는 제거 방법을 사용하고 $ 아약스 함수를 호출하기 전에, 그것을 잘 작동하고 있지만 성공 내부를 걸었습니다하지 않을 때. Servlet 파일에 의해 반환되는 출력을 확인했습니다. 괜찮 았어. 어떤 아이디어?

+0

가 $의 범위가 (이) 때 성공을위한 함수 내에서 변경되지 않습니다

아래 코드를 시도? –

답변

2

:의 상황 때문에 여기

$.ajax({ 
    ... 
    context: this, // set the context for all ajax-related callbacks 
    success: function(data) { 
     $(this).next('.pclass').remove(); 
    } 
}); 
1

ajax.success 메서드 this이 (가) 귀하의 요소를 가리키고 있지 않습니다.

추가하는

var self = this; 
var c = confirm("You sure want to delete this?"); 

같은 코드의 다음

if(data.update == "success"){ 
    $(self).next('.pclass').remove(); 
    $(self).prev('.rurl').remove(); 
    $(self).remove(); 
} 
1

당신은 잃고 범위 "이"당신은 성공의 기능에있을 때. 변수에 $ (this)를 설정하고 변수를 전달할 수 있습니다.

1

성공 콜백 내의 this은 아마도 ajax 호출 외부와 마찬가지로 클릭 된 요소를 참조하지 않는다고 생각합니다. 성공 라인을 자신의 함수로 옮기고, 성공에서 함수를 호출하고, 그 라인의 파이어 버그에 브레이크 포인트를 띄운 후 this이 무엇인지 확인하십시오.

당신은 변수에 컨텍스트를 저장하고 성공 기능이 합격과 같이해야한다
1

문제 : 또는

var that = $(this); 
$.ajax({ 
    ... 
    success: function(data) { 
     that.remove(); 
    } 
}); 

, 당신은 $.ajax's context option을 사용할 수 있습니다 성공 핸들러가 실행될 때 변경됩니다. 그 트릭을 사용하십시오.

$(".delete").bind("click",function(){ 
    var c = confirm("You sure want to delete this?"); 
     //################################# 
     //Capture the context of this so that it can be used in callbacks 
    var that = $(this); 
    if(c){ 
     var text = $(this).prev('.rurl').text(); 
     var idvalue = $(this).prev('.rurl').attr('id'); 
     var id = idvalue.split("|"); 

     $.ajax({ 
        type: "POST", 
        url: "http://localhost:8080/cPEP_UI/Engine_rurl_delete", 
        data: "text="+text+"&eid="+id[1], 
        dataType: "json", 
        success: function(data) { 
         if(data.update == "success"){ 
         //########################## 
         //Since the context of this was stored in that, use it. 
         that.next('.pclass').remove(); 
         that.prev('.rurl').remove(); 
         that.remove(); 
         } 
        }, 
      error:function(xhr,err){ 
       $('#show').show(); 
       $('#show').html("responseText: "+xhr.responseText); 
      } 
     }); 
    } 
}); 
1
$(".delete").bind("click",function(){ 
      var c = confirm("You sure want to delete this?"); 
      if(c){ 
       /* $(this).next('.pclass').remove(); 
       $(this).prev('.rurl').remove(); 
       $(this).remove();*/ 


       var myElement = $(this); 


       var text = $(this).prev('.rurl').text(); 
       var idvalue = $(this).prev('.rurl').attr('id'); 
       var id = idvalue.split("|"); 

       $.ajax({ 
         type: "POST", 
         url: "http://localhost:8080/cPEP_UI/Engine_rurl_delete", 
         data: "text="+text+"&eid="+id[1], 
         dataType: "json", 
         success: function(data) { 
          if(data.update == "success"){ 
           myElement.next('.pclass').remove(); 
           myElement.prev('.rurl').remove(); 
           myElement.remove(); 

           //$(this).next('.pclass').remove(); 
           //$(this).prev('.rurl').remove(); 
           //$(this).remove(); 
          } 
          // $('#show').show(); 
          //$('#show').html(data.update+" "+data.message).fadeOut(8000); 
         }, 
        error:function(xhr,err){ 
         //alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); 
         $('#show').show(); 
         // alert(xhr.responseText); 
         $('#show').html("responseText: "+xhr.responseText); 

        } 

        }); 
      } 
     }); 
관련 문제