2009-12-29 2 views
1

안녕을 사용하여 객체에 전달 잘 작동 다음과 같은 기능이 있습니다이 HTML 코드에는 jQuery를

 jQuery('.btnRemoveItem').click(function(obj){     
      jQuery(this).closest('li').fadeOut(400, function() { $(this).remove(); }); // This works 
     }); 

:

<li id="listItem_dsc_6440.jpg"> 
    <div class="buttonPanel"> 
     <span title="Delete image" class="btnRemoveItem floatRight" id="dsc_6440.jpg"> </span> 
    </div> 
</li> 

이 목록의 항목을 페이드가, 다음을 제거합니다. 이것은 물론

 jQuery('.btnRemoveItem').click(function(obj){ 

      jQuery.post("wp-content/themes/storelocator/include/adm_gallery.php", { deleteImage: 'single', name: jQuery(this).attr('id') }, 
      function(data){ 
       if(data.status == 'deleted'); 
       { 
        jQuery(obj).closest('li').fadeOut(400, function() { $(this).remove(); }); 
       } 
      }, "json"); 
     }); 

는,이 작동하지 않습니다 .post 내부
지금 나는 jQuery.post를 추가 그리고 난 제거/페이드 아웃을 넣어해야합니다. 나는 그것이 obj이 무엇인지 모르기 때문에 그것이라고 확신합니다. 어떻게 사용할 수 있습니까?

아무도 도와 줄 수 있습니까?

답변

1

로컬 변수에 this을 저장하십시오. 이 같은

jQuery('.btnRemoveItem').click(function(){ 
     var obj = this; //reference to the button 
     jQuery.post("wp-content/themes/storelocator/include/adm_gallery.php", { deleteImage: 'single', name: jQuery(this).attr('id') }, 
     function(data){ 
      if(data.status == 'deleted'); 
      { 
       jQuery(obj).closest('li').fadeOut(400, function() { $(obj).remove(); }); 
      } 
     }, "json"); 
    }); 

포스트의 콜백 함수는 폐쇄를 형성하며 외부 함수의 로컬 변수를 "볼"수있다. 당신의 obj 이후

+0

Thos가 훌륭하게 작동했습니다. 감사! – Steven

0

JSON 응답을 반환 할 때 '삭제'페이지가있는 쉬운 방법은 도 삭제 된 개체에 대한 ID/기타 식별 속성을 반환하는 것입니다. 그런 다음 제거 할 개체를 찾으려면 jQuery('#' + data.object_id)을 사용하면됩니다.

일반적으로 object-1, object-2 등과 같은 ID를 사용하여 코드를 작성합니다. 그런 다음 수정하려는 HTML 항목을 검색 할 때 코드에서 #object-[foo]을 찾습니다.

0

난 당신이 jQuery('.btnRemoveItem').closest('li')

+0

하나의 .btnRemoveItem 요소 만 존재하는 경우 솔루션이 작동합니다. className이 더 많은 요소가 있으면 솔루션이 모두 영향을 미칩니다. – Rafael

+0

나는 약 15 개의'.btnRemoveItem'; o) – Steven

+0

옙 - 하나 이상이 존재하면'this'에 지역 변수를 할당하면 효과적이다. – Bostone

0

처럼 뭔가 2 기능에 jQuery(obj).closest('li')를 대체 할 수 있다고 가정하면 다음에 클릭 버튼입니다이 오류는 인 ("게시물"의 마지막 매개 변수를 제거 할 경우에도 발생합니까 현재 "json")? 경우에 따라 반환 유형이 예상과 다른 경우 콜백 메서드가 호출되지 않습니다. 만약

jQuery(obj.target).closest('li').fadeOut(...); 

을 대상 속성을 사용하거나 요소에 대한 참조를 보유 클릭 처리기 내부 헬퍼 변수를 정의 할 수 있도록

1

클릭의 최초의 파라미터() 메소드는, 이벤트 오브젝트이다

jQuery('.btnRemoveItem').click(function(obj){ 
    var element = this; 

    jQuery.post(... 
     function(data){ 
     if(data.status == 'deleted'){ 
      jQuery(element).closest('li').fadeOut(...); 
     }, 
     }, 'json'); 
});