2012-05-11 42 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的如何:觸發點擊) –

回答

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

雖然click事件處理程序綁定到該文件,它不會被觸發,因爲這是事件的代表團。 –

+0

如果有多個'.btn-modal'元素,這是很危險的,因爲代碼會爲每個元素運行一次。我建議改變'$('。btn-modal')。click();'到'$('。btn-modal:first')。click();',就像我的答案。 – jmar777

2

只需添加到您的$(document).ready()處理程序結束:[?jQuery的觸發點擊]

$('.btn-modal:first').click();