在JSP中,可以使用以下幾種方式提交表單:
<form action="submit.jsp" method="get">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
<form action="submit.jsp" method="post">
<input type="text" name="username">
<input type="submit" value="Submit">
</form>
submit()
方法提交表單。例如:<form id="myForm" action="submit.jsp" method="post">
<input type="text" name="username">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm() {
document.getElementById("myForm").submit();
}
</script>
<form id="myForm">
<input type="text" name="username">
<input type="button" value="Submit" onclick="submitForm()">
</form>
<script>
function submitForm() {
var form = document.getElementById("myForm");
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "submit.jsp", true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 處理服務器響應
}
};
xhr.send(formData);
}
</script>
這些是常見的表單提交方式,在實際應用中可以根據需求選擇合適的方式。