<form action="/form1" method="post" onsubmit="return check()">
<h1>
留言
</h1>
企业名称
<input type="text"name="enterprise" id="enterprise">
<br>
移动电话
<input type="text" name="phone" id="phone">
<br>
<input type="submit">
</form>
<script>
function check() {
var aa=Document.getElementById("enterprise").value;
if(aa==""){
alert("信息为空!")
return false;
} else{
return true;
}
}
@WebServlet(name="Form1Servlet",urlPatterns = "/form1")
public class Form1Servlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
String enterprise=request.getParameter("enterprise");
String phone=request.getParameter("phone");
response.sendRedirect("/form1Result.html");
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
}
运行之后不管输入与否都会跳转到form1Result.html, onsubmit="return check()"这句话好像没有效果
但我想想让enterprise不为空时才跳转该、enterprise为空时不跳转,应该怎么改动