2016-08-14 112 views
-6

當有人點擊我的標籤時,我想打開一個像元素一樣的小「警報」,我希望能夠使用模式的按鈕功能。用jquery創建模態對話框

是否有一個jQuery庫/擴展,可以輕鬆地做到這一點?

$(document).on('click', '#MyLabel', function() { 

    openModal(); 

}); 
+1

'alert(「顯示您的消息」)' –

+0

甚至jQuery對話框 –

+0

我不知道這個事件名稱是什麼? –

回答

1

這是一個在jQuery UI中從根本上簡單的任務。見jsfiddle

$("#myDialog").dialog({ 
    autoOpen : false, 
    modal  : true, 
    title  : "A Dialog Box", 
    buttons : { 
       'OK' : function() { 
        var textValue = $('#myTextBox').val(); 
        alert('The value of the text box is ' + textValue); 
        //Now you have the value of the textbox, you can do something with it, maybe an AJAX call to your server! 
       }, 
       'Close' : function() { 
        alert('The Close button was clicked'); 
        $(this).dialog('close'); 
       } 
       } 
}); 
+0

我怎樣才能讓女巫按鈕點擊? –

+0

看看這個:[jsfiddle](https://jsfiddle.net/eDMmy/453/) –

+0

謝謝你,先生,我知道了 –

2

一個簡單,快捷的方式可能會使用一些UI框架一樣Bootstrap,具有開箱即用HTML組件。在您的應用中包含框架之後,您可以複製&粘貼模板並顯示模式,而不是。

在你的js腳本:

$(document).on('click', '#MyLabel', function() { 

    $('#MyModal').show(); 

    // do stuff like button click callback: 
    $('.approve').on('click', function(){ 
     alert('approved'); 
    } 

}); 

對於您可以使用這個例子從Boostrap's documentation採取的模板:

<div class="modal fade" id="myModal" role="dialog"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 

      <div class="modal-header"> 
       <button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button> 
       <h4 class="modal-title">Modal title</h4> 
      </div> 

      <div class="modal-body">Modal body</div> 

      <div class="modal-footer"> 
       <!-- Approve Button --> 
       <button class="approve btn btn-primary" type="button">Save changes</button> 
      </div> 
     </div> 
    </div> 
</div> 

希望這有助於!

順便說一句 - 你在stackoverflow。將來會更具描述性,並做一些調查以避免投票☺祝你好運! ☺

+0

其模型內的按鈕。它不會在'onClick'函數上工作 –

+0

嗨@ZainFarooq,看到我上面的更新。它涵蓋了按鈕的功能 – DotBot

+1

我也編輯了這個代碼...如果你的工作正常。你可以繼續你的 –