首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

JavaScript 動態修改表單 action 實現按鈕跳轉不同 Servlet

     編輯:程式設計疑難解答

實現代碼

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") 等獲取參數。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved