2011-11-04 51 views
2

我有我的網頁上下面的代碼:如何顯示像stackoverflow對話框?

$('#Report').click(Report); 

    function Report() { 
     var e = encodeURIComponent, 
       arr = [ 
       "dataSource=" + e($('#DataSource').val()), 
       "statusID=" + e($('#StatusID').val()) 
       ]; 
     window.location.href = '/Administration/Tests/Report?' + arr.join("&"); 
     return false; 
    } 

當使用id =報告按鈕用戶點擊,則函數被調用,它顯示了報告信息的頁面。

但是,如果未設置dataSource和statusID,那麼我的代碼將以異常結束。

是否有某種方式可以檢查DataSource的值不等於「00」且StatusID不等於「0」,然後顯示一個對話框,告訴用戶應該選擇這些字段。

理想情況下,我想有像Stackoverflow使用的對話框的東西。

+0

哪個計算器對話框OD,你有什麼想法? – Michal

回答

0

我不確定000代表您的請求,但我把它們放在那裏。我不熟悉堆棧溢出對話框,但這裏有一些起始代碼可以使用。外部div(#error-frame)可防止在顯示框時點擊任何其他元素。

演示:http://jsfiddle.net/ThinkingStiff/CW5wW/

腳本:

function Report() { 

    var dataSource = $('#DataSource').val(), 
     statusId = $('#StatusID').val(); 

    if(dataSource && statusId && dataSource != '00' && statusId != '0') { 

     var e = encodeURIComponent, 
      arr = [ 
      "dataSource=" + e(dataSource), 
      "statusID=" + e(statusId) 
      ]; 

     window.location.href = '/Administration/Tests/Report?' + arr.join("&"); 

    } else { 
     //error msg here, perhaps: 
     showError('Both Data Source and Status ID are required.'); 
    }; 

    return false; 

} 

function showError(message) { 

    document.getElementById('error-message').textContent = message; 
    document.getElementById('error-frame').style.display = 'block';  

    var error = document.getElementById('error'); 

    error.style.left = ((window.innerWidth/2) - (error.scrollWidth/2)) + 'px'; 
    error.style.top = ((window.innerHeight/2) - (error.scrollHeight/2)) + 'px'; 

}; 

HTML:

<div id="error-frame"><div id="error"><div id="error-message"></div><button onclick="this.parentNode.parentNode.style.display='none'">OK</button></div></div> 

CSS:

#error-frame { 
    display: none; 
    height: 100%; 
    left: 0; 
    position: absolute; 
    top: 0; 
    width: 100%; 
    z-index: 9999; 
} 

#error { 
    border: 4px solid darkgray; 
    height: 100px; 
    padding: 4px; 
    position: relative; 
vwidth: 200px; 
} 

#error button { 
    position: absolute;  
    right: 0; 
    bottom: 0; 
} 
+0

我在想的那個對話框是一個出現在屏幕上的灰色邊框,我只是嘗試了一些不同的東西,無法讓這種盒子出現。我認爲如果我試圖在一天中提出太多問題或試圖經常提問,就會出現這種情況。 –

+0

我添加了一個片段來玩對話框。 – ThinkingStiff