頁面中「查詢」與「返回」按鈕點擊無反應,通常由以下問題導致:
<script src="jquery.min.js"> 路徑正確,且在自定義腳本之前載入。#search vs #serach)或元素尚未渲染時綁定。query("select * from emp where deptno=" + deptno + "") 缺少右引號)。<input type="submit"> 會觸發表單提交導致頁面重載,需在事件中呼叫 event.preventDefault() 或改用 type="button"。<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
$("#back").click(function(e) {
e.preventDefault();
location.href = "welcome.html";
});
$("#search").click(function(e) {
e.preventDefault();
var deptno = $("#deptno").val();
// 前端不應直接拼接 SQL,此處僅示範邏輯
// 實際應透過 AJAX 呼叫後端 API
if (deptno) {
sessionStorage.setItem('deptno', deptno);
location.href = "get_dep_emp.html";
}
});
});
</script>
console.log('clicked') 確認事件是否觸發。type="button" 避免表單自動提交。const/let、箭頭函數提升可讀性。