2008-12-27 79 views
2

我在jQuery中使用SimpleModal,並且我有一個確認對話框。如果結果是Yes,我必須在此對話框中調用my.php。但是,我已經完成了代碼,而且我仍然在尋找想法。我該怎麼做?jQuery中的簡單模態

$(document).ready(function() { 
    $('#confirmDialog input.confirm, #confirmDialog a.confirm').click(function (e) { 
     e.preventDefault(); 
     // Example of calling the confirm function. 
     // You must use a callback function to perform the "yes" action. 
     confirm("Continue", function() { 
      alert("OK"); 
     }); 
    }); 
}); 

function confirm(message, callback) { 
    $('#confirm').modal({ 
     close:false, 
     position: ["20%",], 
     overlayId:'confirmModalOverlay', 
     containerId:'confirmModalContainer', 
     onShow: function (dialog) { 
      dialog.data.find('.message').append(message); 

      // If the user clicks "yes" 
      dialog.data.find('.yes').click(function() { 
       $.get('my.php', function(data){ 
        // Create a modal dialog with the data. 
        // Here: How do I write the same window? 
       }); 

       // Call the callback 

       // Close the dialog 
       $.modal.close(); 
      }); 
     } 
    }); 
} 

在這裏,我有問題,如何從Ajax結果寫入同一個窗口Confirmdialog。我該怎麼做?

回答

2

我不知道了確認功能最適合您的需求,但這樣的事情應該工作:

function confirm(message, callback) { 
    $('#confirm').modal({ 
     close:false, 
     position: ["20%",], 
     overlayId:'confirmModalOverlay', 
     containerId:'confirmModalContainer', 
     onShow: function (dialog) { 
      dialog.data.find('.message').append(message); 

      // If the user clicks "yes" 
      dialog.data.find('.yes').click(function() { 
       $.get("my.php", function (data) { 
        /* Sample response: 
        * <div id="title">my title</div> 
        * <div id="message">my message</div> 
        * 
        */ 
        var resp = $("<div/>").append(data); 
        var title = resp.find("#title").html(), 
         message = resp.find("#message").html(); 

        dialog.data.find(".header span").html(title); 
        dialog.data.find(".message").html(message); 
        dialog.data.find(".buttons .yes").hide(); 
        dialog.data.find(".buttons .no").html("Close"); 

        // No need to call the callback or $.modal.close() 
       }); 
      }); 
     } 
    }); 
} 
+0

感謝易買得的提醒我 – venkatachalam 2008-12-29 04:40:02

1

我不確定你想完成什麼 - 你是否試圖重用確認模式對話框來顯示你的結果?我想你可以這樣做,因爲你只需在對話框中有一個「關閉X」按鈕,只需用你的結果替換消息的內容,然後移除按鈕,這樣你的回調就不會再被觸發。它可能看起來像這樣:

dialog.data.find('.message').html('new contents from your ajax data'); 
dialog,data.find('.buttons').remove(); 

但是,這似乎是對我的模態對話的濫用。在我看來,對話框應該只包含與用戶的單一交互。如果您需要基於初始對話框的結果進一步交互,那麼我會考慮添加另一個模式對話框,在當前AJAX結果關閉之後彈出,或將AJAX結果插入主界面並處理它那裏。