2013-06-23 2 views
0

jQuery 대화 상자를 사용하여 다른 페이지를 기본 페이지의 대화 상자에로드합니다. 다른 페이지에는 앵커 태그가있을 수 있으며로드 함수의 completed 이벤트를 사용하여 내용이로드 된 div의 모든 앵커 태그를 선택합니다. 그런 다음 기본 페이지에 포함 된 div에 내용이로드되도록 앵커 태그 클릭 이벤트 처리기를 연결합니다. 그러나 이것은 두 번만 작동합니다. 아래 예제 코드를 실행하면 Partial1이 대화 상자에 나타납니다. 대화 상자에서 Partial2 링크를 클릭하면 Partial1이 대화 상자에로드되지만 이번에는 Partial2 링크를 클릭하면 기본 페이지에로드됩니다. 내가 뭘 잘못하고/또는 파악하지 못하고 있습니까? 자바 스크립트 이벤트 및 재귀 문제

홈/색인 :

<a href="/Home/Partial1" id="help">Partial 1</a> 

<div id="dialog" style="display: none"> 

</div> 


<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#help").click(function() { 
      adjustNestedAnchors($(this)); 
      $("#dialog").dialog().show(); 
      return false; 
     }); 

     function adjustNestedAnchors(element) { 
      $("#dialog").load($(element).attr("href"), 
       function (response, status, xhr) { 
        if (status != "error") { 
         $("#dialog a").click(function() { 
           $("#dialog").load($(this).attr("href"), adjustNestedAnchors($(this))); 
           return false; 
         }); 
        } 
       }); 
     } 
    }); 
</script> 

홈/Partial1

This is a sample partial view, which has a link back to <a href="/Home/Partial2">Partial2</a>. 

홈/Partial2

This is a sample partial view, which has a link back to <a href="/Home/Partial1">Partial1</a>. 

답변

2

문제 이다이 라인이 :

$("#dialog").load($(this).attr("href"), adjustNestedAnchors($(this))); 

divload를 호출 전에 링크 adjustNestedAnchors를 호출하기 때문에 중첩 된 앵커에게 내용이로드 된을 조정 아무것도 없다.

<a href="/Home/Partial1" id="help">Partial 1</a> 

<div id="dialog" style="display: none"> 

</div> 


<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#help").click(function() { 
      loadDialog(this); 
      $("#dialog").dialog().show(); 
      return false; 
     }); 

     // load link in dialog, and adjust its links to load inside it 
     function loadDialog(link) { 
      $("#dialog").load($(link).attr("href"), function (response, status, xhr) { 
       $("#dialog a").click(function() { 
        loadDialog(this); 
        return false; 
       }); 
      }); 
     } 
    }); 
</script> 

(면책 조항 : :. 테스트하지) 내가 loadDialogadjustNestedAnchors 이름을 변경 한

참고, 내가 더 생각

대신, 나는 당신이 원하는 것은이 같은 믿습니다 그것의 1 차 기능의 정확한 묘사.

+0

위대한 작품입니다! 내 오류 및 함수 이름 제안을 지적 해 주셔서 감사합니다! – codechurn

+0

@Art : 오신 것을 환영합니다! – ruakh

관련 문제