2012-05-11 5 views
1

다음 코드는 사용자가 링크를 클릭 할 때만로드됩니다.Onload 링크를 클릭하십시오.

사용자가 클릭하지 않아도 페이지가로드되기를 원합니다.

<script> 
$(document).ready(function() { 
    // Support for AJAX loaded modal window 
    $(document).on('click', '.btn-modal', function() { 
     $.get($(this).attr('data-target'), function(data) { 
      $('#modal').html(data); 
      $('#modal').modal('show'); 
     }, 'html'); 
    }); 
});​ 
</script> 

<!-- Modal window to load dynamic content --> 

<div class="modal hide fade customDialog" id="modal"></div> 
<a class="btn btn-modal" data-target="page.html" >Launch Modal</a> 
+0

가능한 중복 (http://stackoverflow.com/questions/6158532/jquery-how-to-trigger-click) –

답변

4
$(document).ready(function() { 

     // Support for AJAX loaded modal window 
     $(document).on('click','.btn-modal',function() { 

      $.get($(this).attr('data-target'), function(data) { 
      $('#modal').html(data); 
      $('#modal').modal('show'); 
      }, 'html'); 
     }); 

     // here write 
     $('.btn-modal').click(); 
    // if you have multiple button then keep filter 
    // eg. 
    // $('.btn-modal:eq(0)').click(); // for first button 
    // $('.btn-modal:eq(1)').click(); // for second button and so more 
}); 
+1

클릭 이벤트 핸들러는 문서에 바인딩되지만 이벤트 위임이므로 클릭 이벤트 핸들러가 트리거하지 않습니다. –

+0

'.btn-modal' 요소가 여러 개있는 경우 위험합니다. 각 요소에 대해 코드가 한 번 실행되기 때문에 위험합니다. 내 대답과 마찬가지로'$ ('.btn-modal'). (();'() .btn-modal : first ') click(); – jmar777

2

은 그냥 $(document).ready() 핸들러의 끝이 추가 [? JQuery와 트리거를 클릭하는 방법]

$('.btn-modal:first').click(); 
관련 문제