当前位置: 代码迷 >> Java Web开发 >> 为何表单提交的时候不走onsubmit里的函数
  详细解决方案

为何表单提交的时候不走onsubmit里的函数

热度:92   发布时间:2016-04-14 20:38:43.0
为什么表单提交的时候不走onsubmit里的函数
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%> 
 <%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>
<script type="text/javascript">

   function checkname(){
   document.getElementById("username_prompt").innerHTML="";
   var username=document.getElementById("username").value;
   if(username==""){   
  var username_prompt= document.getElementById("username_prompt");
  username_prompt.innerHTML="用户名不能为空";
   }
   }
   
   function checkpassword(){
   document.getElementById("password_prompt").innerHTML="";
   var password=document.getElementById("password").value;
   if(password==""){
   document.getElementById("password_prompt").innerHTML="密码不能为空";
   }
   
 function check(){
var username=document.getElementById("username").value;
                               var password=document.getElementById("password").value;
if(username==""){
alert("username不能为空");
return false;
}
 if(password==""){
 alert("密码不能为空");
 return false;
 }
                         return true;
}
   }
</script>
</head>
 
<body>
<div class="login-top"></div>
<div class="login-area">
<form action="login" method="post" onsubmit="return check();">
<label>
工&nbsp;&nbsp;号:
</label>
<input type="text" name="user.name" id="username" onblur=" checkname();"/><font color="red" id="username_prompt"></font>
<label>
密&nbsp;&nbsp;码:
</label>
<input type="password" name="user.password" id="password" onblur="checkpassword();"/><font color="red" id="password_prompt"></font>

<input type="submit" class="login-sub" value="" id="login" />

<br/>
<div id="#msg">
<b><font color="red"><s:property value="#session.message" escape="false"/></font></b>
</div>
</form>
</div>
<div class="login-copyright"></div>
</body>
</html>
 表单提交的时候直接不走里的js的check()的验证方法,from 表单里的onsubmit="return check();"一点作用都没有,不知道为什么,帮忙看看哪里错了
------解决思路----------------------
你的check方法定义在checkpassword方法之内了
将check方法定义到外面再试试看
------解决思路----------------------
IE浏览器 javascript调试 http://blog.csdn.net/magi1201/article/details/43927915

火狐浏览器javascript调试 http://blog.csdn.net/magi1201/article/details/43990621