2016-08-30 80 views
1

我需要在用戶點擊模式中的「X」時處理按鈕。這是我的嘗試:我如何處理x按鈕模式

var modal = bootbox.dialog({ 

    message: dataTopicRilievo, 
    title: 'Selezionare data topic in rilievo', 
    buttons: { 
    success: { 
    label: "Save", 
    className: "btn-primary save", 
    callback: function() { 

     console.log("HIII"); 

    }, 
    close: function(event,ui) { 
    console.log("CLOSEEEEEEEEEEEEE");  
    } 
}); 

但是這第一個版本不工作,所以我用:

modal.on('hidden.bs.modal', function (e) { 

    console.log("CLOSEEEEEEEEEEEEE"); 

}); 

這部分代碼是不正確的,因爲調用模式的方法也從隱藏積極的行動。任何人都可以幫助我?

+0

你用什麼版本的引導呢?如果你想要更具體的按鈕,你可以使用jQuery選擇器來查找DOM中的節點元素並編寫你需要的代碼。在v3中它是'$('selector')。modal()'not'.dialog()' – Justinas

+0

你可以嘗試使用類似$('modal modal-body bootbox-close-button')。click(function(){// your_code}); .... Bootstrap調用是來自某些「preseted」事件處理的抽象 – RPichioli

回答

1

嘗試結合事件,直接關閉按鈕:

$(document).ready(function() { 
 
    $('.modal [data-dismiss="modal"]').click(function() { 
 
    console.log('Closed modal'); 
 
    }); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 

 
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal"> 
 
    Launch demo modal 
 
</button> 
 

 
<!-- Modal --> 
 
<div class="modal fade" id="myModal"> 
 
    <div class="modal-dialog"> 
 
    <div class="modal-content"> 
 
     <div class="modal-header"> 
 
     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span></button> 
 
     <h4 class="modal-title">Modal title</h4> 
 
     </div> 
 
     <div class="modal-body"> 
 
     TEST 
 
     </div> 
 
     <div class="modal-footer"> 
 
     <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>