`id` 用於**唯一標識表單**,方便 JavaScript/CSS 選取(如 `document.getElementById("formId")`、`$("#formId")`)。`name` 用於表單提交時標識數據鍵值,兩者用途不同,可共存。
格式:`action="${pageContext.request.contextPath}/Servlet映射路徑"`。
例子:`web.xml` 配置 `
<form action="${pageContext.request.contextPath}/ServletTest2" method="post">
帶參數:`action="${pageContext.request.contextPath}/ServletTest2?id=123&name=abc"`。
`onclick` 是**滑鼠點擊事件屬性**,值為 JavaScript 函數呼叫(如 `onclick="validateForm()"`)。在 `` 上常用於提交前驗證:`onclick="return check();"`,若 `check()` 返回 `false` 則阻止提交。
<form id="loginForm" name="loginForm" action="${pageContext.request.contextPath}/LoginServlet" method="post" onsubmit="return validate()">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="登入" />
</form>
<script>
function validate() {
var u = document.forms["loginForm"]["username"].value;
if (!u) { alert("請輸入帳號"); return false; }
return true;
}
</script>