2017-06-05 26 views
0

我遇到以下函數的問題。我正在動態創建一個引導模式(常規div),並且我想附加一個函數,我通過一個變量(巫婆工作正常)並運行它(「隱藏」),但是隻要模態($(modal).on('hide',windowclose_function);)。有什麼我錯過了嗎?隱藏一個動態元素函數。('hide')

感謝

function openModalFromUrl(modal_url, close_function = function(){}) { 

    var token = $("meta[name='csrf-token']").attr("content"); 

    $.ajax({ 
     data: { 
      '_token': token, 
     }, 
     url: modal_url, 
     type: 'POST', 
     success: function(html) { 

      // create a new modal div 
      var modal = document.createElement("div"); 
      $(modal).addClass('modal fade'); 
      $(modal).attr("role", "dialog"); 

      $('body').append(modal); 

      // place response in the modal 
      $(modal).html(html) 

      // open the modal 
      $(modal).modal('show'); 

      // THIS FIRES IMMEDIATELY 
      $(modal).on('hide', window[close_function]()); 

     }, 

     error: function() { 
      bootbox.alert('Error'); 
     } 

    }); 

} 
+0

不應該是'$(模態).on('hide',close_function);'? – PeterMader

+3

'$(模式)。在(「隱藏」,窗口[close_function]);''刪除()'只是傳遞函數引用 – Satpal

回答

2

的問題是在這條線: -

$(modal).on('hide', window[close_function]()); 

將其更改爲: -

$(modal).on('hide', window[close_function]); 

說明: -

在JavaScript中,當我們提供了一個功能作爲參考,我們使用它的名字沒有任何括號

即 'my_func,並將' 而不是 'my_func,並將()'

由於'my_func()'將函數的結果作爲參考而不是函數本身。

所以只要刪除括號,你的問題就解決了。

+0

感謝您的回答...但它似乎沒有工作...這裏是我已經改變了ADN現在測試,它不會似乎都來調用隱藏的功能(我試着用'隱藏'和'hidden.bs。模態」事件) '變種test_function =函數(){ 警報( 'AAA'); } //重置靠近 $(模態)。在( 'hide.bs.modal',test_function)的功能; ' –

+0

看看這裏.... https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal_event_hide&stacked=h 如果它仍然無法工作...那麼請檢查您的引導js和CSS文件。 –

+0

這是問題,那在div動態創建這樣的事實,我不得不使用 '$(「身體」)上(「hide.bs.modal」,莫代爾,窗口[close_function]);' 標誌以此爲答案,謝謝! –

0

按照documentation你應該使用hidden.bs.modal事件。也不需要檢查該函數的window對象,只需傳遞它即可。

$('#myModal').modal('show'); 
 

 
var close_function = function() { 
 
    console.log('closed'); 
 
} 
 

 
$('#myModal').on('hidden.bs.modal', close_function);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" /> 
 
<div class="modal fade" id='myModal' abindex="-1" role="dialog"> 
 
    <div class="modal-dialog" role="document"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     <p>One fine body&hellip;</p> 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     <button type="button" class="btn btn-primary">Save changes</button> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

感謝您的快速解答......問題是,該功能可有不同的名稱。我使用的按鈕功能名稱傳遞給模式(例如,數據回調=「refresh_list」),我想調用該函數 –

+0

,但你把它傳遞給openModalFromUrl,當地parammeter總是有close_function –

+0

我有一個打開openModalFromUrl函數的動態按鈕。而這個按鈕有數據網址是AJAX模式的URL,以及一個數據close_function即當關閉模式應該運行該功能的名稱。我不知道我是否可以從按鈕傳遞函數引用? ('click','.modal_url',function(){ ' openModalFromUrl($(this).data('url'),$(this).data('close_function' )); });' –

0

謝謝大家的回答。我找到了解決方案。 由於模態進行動態生成的,我不得不改變調用:

$('body').on('hide.bs.modal', modal, window[close_function]); 

現在,它就像一個魅力。再次感謝大家!