当前位置: 代码迷 >> JavaScript >> 为啥onsubmit返回false,表单还是提交
  详细解决方案

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

热度:550   发布时间:2013-10-27 15:21:50.0
为什么onsubmit返回false,表单还是提交
<%@ 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 = "登陆">&nbsp;<input type = "reset" value = "重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
javascript 表单

------解决方案--------------------
document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!";
f.userid.focus() ;     //你保证这里正确执行了?
return false ;
  相关解决方案