2017-04-18 43 views
1

我想顯示一個對話框,只有一個確定按鈕上的ajax調用的響應。當用戶點擊確定時,它應該重新加載頁面。但是現在頁面重新加載會在彈出對話框後立即發生。它不會等待用戶單擊確定。僅供參考我正在使用Jquery Modal對話框。在Jquery Modal對話框中單擊確定按鈕時觸發事件

簡單的瀏覽器alert()爲我完成這項工作,但我不喜歡alert()的外觀。

任何幫助,高度讚賞!

$.ajax({ 
    url: "modules/mymod/save.php", 
    type: "POST", 
    data: $('#requestForm').serialize(), 
    statusCode: {404: function() {alert('page not found');}}, 
    success: function (data) { 
     // alert(data); 
     modal({type: 'alert', title: 'Alert', text: data}); 
     window.location.href = window.location.href; 
    } 
}); 
+0

http://stackoverflow.com/questions/33029730/open-a-popup-box-after-receiving-result-from-ajax –

回答

0

Reference:

  $.ajax({ 
       url: "modules/mymod/save.php", 
       type: "POST", 
       data: $('#requestForm').serialize(), 
       statusCode: {404: function() {alert('page not found');}}, 
       success: function (data) { 
        // alert(data); 
        modal({ 
         type: 'alert', 
         title: 'Alert', 
         text: data, 
         buttons: [{ 
           text: 'OK', //Button Text 
           val: 'ok', //Button Value 
           eKey: true, //Enter Keypress 
           addClass: 'btn-light-blue btn-square', //Button Classes 
           onClick: function() { 
            window.location.href = window.location.href; 
           } 
          }, ], 
         center: true, //Center Modal Box? 
         autoclose: false, //Auto Close Modal Box? 
         callback: null, //Callback Function after close Modal (ex: function(result){alert(result);}) 
         onShow: function(r) { 
          console.log(r); 
         }, //After show Modal function 
         closeClick: true, //Close Modal on click near the box 
         closable: true, //If Modal is closable 
         theme: 'xenon', //Modal Custom Theme 
         animate: true, //Slide animation 
         background: 'rgba(0,0,0,0.35)', //Background Color, it can be null 
         zIndex: 1050, //z-index 
         buttonText: { 
         ok: 'OK', 
         yes: 'Yes', 
         cancel: 'Cancel' 
        }, 
        template: '<div class="modal-box"><div class="modal-inner"><div class="modal-title"><a class="modal-close-btn"></a></div><div class="modal-text"></div><div class="modal-buttons"></div></div></div>', 
        _classes: { 
        box: '.modal-box', 
        boxInner: ".modal-inner", 
        title: '.modal-title', 
        content: '.modal-text', 
        buttons: '.modal-buttons', 
        closebtn: '.modal-close-btn' 
         } 
        }); 
        } 
      }); 
+0

@AdityaAmit嘗試我的解決方案。希望它能在你的情況下工作。謝謝 –

+0

我試過你的解決方案,但按鈕點擊事件不會觸發。只有默認行爲即將到來。 –

+0

@AdityaAmit你需要** [檢查此鏈接](https://github.com/CreativeDream/jquery.modal/blob/master/index.html)**。 –

0

您可以通過dialog模式buttons屬性,每一個註冊的事件,就像這樣:

$.ajax({ 
    url: "modules/mymod/save.php", 
    type: "POST", 
    data: $('#requestForm').serialize(), 
    statusCode: {404: function() {alert('page not found');}}, 
    success: function (data) { 

     $("#dialog-confirm").dialog({ 
      resizable: false, 
      height: 200, 
      modal: true, 
      buttons: { 
       Proceed: function() { 
       window.location.href = window.location.href; 
       }, 
       Cancel: function() { 
       // Cancellation code here 
       } 
      } 
     }); 
    } 
}); 
+0

OP不使用jQuery對話框 –

+0

@MayankPandeyz有一個jquery-ui-dialog標籤。 –

+0

@KobyDouek問題是我無法在這裏使用確認對話框。因爲取消沒有任何關係。我只是希望頁面重新加載,等到用戶單擊確定。謝謝。 –

0

簡單的瀏覽器警報()爲我做這項工作,因爲alert()是一個阻塞調用。如果您省略了警報,那麼您的代碼不會與任何事件綁定,以檢查用戶是否單擊按鈕,這就是代碼塊立即執行並重新加載頁面的原因。

所以結合下面的代碼:

window.location.href = window.location.href; 

一些按鈕,點擊裏面,來解決問題。

0

不要在success.instead使用window.location的功能與OK按鈕打開模態的成功(如何做到這一點,我想你已經知道了),並指定一些ID給按鈕讓說id="loction_btn"。 然後使用僅此

$('document').on('click','#location_btn',function(){ 
    window.location.href = window.location.href; 
}); 
相關問題