为什么onsubmit返回false,表单还是提交

q6413260 2013-10-24 09:47:38
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import = "java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录操作</title>
</head>
<body>
<script language="javascript">
function validate(f){
if(!(/^\w{1,9}$/.test(f.id.value))){
document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!";
f.userid.focus() ;
return false ;
}
if(!(/^\w{1,9}$/.test(f.password.value))){
alert("密码必须是1~9位!") ;
f.userpass.focus() ;
return false ;
}
}
</script>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
Map<String,String> message = (HashMap<String,String>) request.getAttribute("message") ;
if(message != null){ // 有信息返回
Set<String> keySet = message.keySet() ;
Iterator<String> iter = keySet.iterator() ;
while(iter.hasNext()){
%>
<h4><%=message.get(iter.next())%></h4>
<%
}
}
%>
<center>
<form action = "LoginServlet" method = "post" onSubmit="return validate(this)">
<h1>用户登录</h1>
<table border = "1">
<tr>
<td>用户ID:</td>
<td><input type = "text" name = "id"></input><span id = "idmsg"></span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type = "password" name = "password"></input><span id = "passwordmsg"></span></td>
</tr>
<tr>
<td colspan = "2"><input type = "submit" value = "登陆"> <input type = "reset" value = "重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
...全文
230 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
KK3K2005 2013-10-25
  • 打赏
  • 举报
回复
document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!"; f.userid.focus() ; //你保证这里正确执行了? return false ;
q6413260 2013-10-25
  • 打赏
  • 举报
回复
引用 2 楼 KK3K2005 的回复:
document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!"; f.userid.focus() ; //你保证这里正确执行了? return false ;
知道了,f.userid.focus();这句话错了,应该是f.id.focus();谢谢了
q6413260 2013-10-24
  • 打赏
  • 举报
回复
补充两张图第一张为登陆输入参数

点登录后出现过“用户名必须是1~9位!”说明js起作用了,应该返回false了,但很快就被提交到LoginServlet,如下图

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧