實現代碼
function go() {
var form = document.forms["top_text_form"]; // 獲取 form
var wafer = document.getElementById("Wnum").value;
var dieType = document.getElementById("Ptype").value;
var actionUrl = "";
if (wafer && !dieType) {
actionUrl = "../SelectInfoByWaferID";
} else if (!wafer && dieType) {
actionUrl = "../SelectInfoByDieType";
} else {
alert("請僅填寫一個查詢條件");
return;
}
form.action = actionUrl;
form.submit();
}
HTML 表單
<form name="top_text_form" method="post">
Wafer ID: <input type="text" id="Wnum" name="waferId">
Die Type: <input type="text" id="Ptype" name="dieType">
<button type="button" onclick="go()">查詢</button>
</form>
關鍵點
- 按鈕設置
type="button" 防止默認提交。
- JS 中先判斷條件,再動態設置
form.action,最後 form.submit()。
- Servlet 端通過
request.getParameter("waferId") 等獲取參數。