<%@ page language="java" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
function checkForm(){
return false;
}
</script>
</head>
<body>
<%
out.println(request.getParameter("name")==null?"":(String)request.getParameter("name"));
%>
<form action="index.jsp">
<input type="text" name="name">
<input type="submit" name="ok" value=" O K " οnclick="return checkForm()" >
</form>
</body>
</html>
以上红色字体只在单击OK按钮时触发,如果在文本框中按回车,页面还是会提交.
如果要在页面提交时判断文本的值,需使用form的onsubmit,如下:
<%@ page language="java" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type="text/javascript">
function checkForm(){
return false;
}
</script>
</head>
<body>
<%
out.println(request.getParameter("name")==null?"":(String)request.getParameter("name"));
%>
<form action="index.jsp" onsubmit="return checkForm()" >
<input type="text" name="name">
<input type="submit" name="ok" value=" O K " >
</form>
</body>
</html>