現代瀏覽器預設攔截 window.open() 彈窗,導致用戶體驗差。使用 jQuery UI Dialog 在頁面內建立模態彈窗,不受攔截器影響,且可完全自定義樣式與互動。
<div id="displayClassDiv" style="display:none;" class="gp_box">
<ul>
<%-- 伺服器端迴圈渲染專案/分期/班級 --%>
<li>
<h1 class="gp_title">
<span id="<%=projectId%>"><%=projectName%></span>
</h1>
<dl class="gp_list">
<dt id="<%=termId%>"><%=termName%></dt>
<dd class="grap">
<%-- 班級單選框 --%>
<input type="radio" name="rd_class" value="<%=classId%>"
id="<%=classId%>" <%=isCurrent ? 'checked' : ''%> />
<%=className%>
</dd>
</dl>
</li>
</ul>
</div>
<script>
// 顯示班級選擇彈窗
function ShowClassInfo() {
$('#displayClassDiv').dialog({
modal: true,
width: 700,
height: 400,
title: '請選擇跳轉班級:',
overlay: { opacity: 0.5, background: 'black' },
buttons: {
'確定': function() { SubmitClassInfo(); },
'取消': function() { $(this).dialog('destroy'); }
},
close: function() { $(this).dialog('destroy'); }
});
}
// 提交選擇並跳轉
function SubmitClassInfo() {
var $checked = $('[name=rd_class]:checked');
var classId = $checked.val();
if (!classId) {
alert('請選擇一個班級');
return;
}
window.location.href = '../ClassPortal/ClassPortal.aspx?CLASS_ID=' + classId;
}
// 觸發按鈕綁定
$('#showClassBtn').click(ShowClassInfo);
</script>
public DataTable dtPTC = new DataTable(); // 專案/分期/班級
protected void Page_Load(object sender, EventArgs e) {
// 獲取當前班級 ID
string classId = Request.QueryString["ClassID"] ?? Session["CLASSID"]?.ToString();
Session["CLASSID"] = classId;
// 獲取用戶權限
string sqlRole = "SELECT Ctype FROM T_PUB_USER_CLASS WHERE ClASSID=" + classId + " AND USERID=" + UserID;
// ... 執行查詢設定 role
// 獲取完整層級資料
getALLInfo();
}
protected void getALLInfo() {
int userId = Convert.ToInt32(Session["USER_ID"]);
SqlParameter[] sp = { new SqlParameter("@USER_ID", userId) };
dtPTC = bdb.RunProcQuery("HZX_SelectCLASS_ByUserID", sp);
}
// 原生 dialog 範例
const dialog = document.getElementById('myDialog');
dialog.showModal(); // 模態
dialog.close(); // 關閉
dialog.addEventListener('close', () => console.log(dialog.returnValue));
使用頁面內模態彈窗替代 window.open,徹底解決瀏覽器攔截問題。jQuery UI Dialog 提供豐富 API,配合伺服器端渲染資料,能快速構建複雜選擇介面。新專案建議評估原生 <dialog> 或輕量庫以降低依賴。