<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'useradd.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
   function submitForm(){
     if(document.form1.username.value=""){
        alert("请输入姓名");
        document.form1.username.focus();
        return false;
    }else if(document.form1.password.value=""){
        alert("请输入密码");
        document.form1.password.focus();
        return false;
    }else{
        return true;
    }
  }
</script>
  </head>
  
  <body>
    <center><h2>增加人员信息</h2> <br>
    <form name="form1" action="/noframe/AddServlet" method="post" >
    人员姓名:<input type="text" name="username">
    登录密码:<input type="password" name="password"><br>
   <input type="submit" name="提交" value="提交" onclick="submitForm()">这里好像不执行页面直接跳转
   <input type="reset"  name="重置" value="清空">
    </form>
    </center>
  </body>
</html>

解决方案 »

  1.   

    把document.form1.username.value
    都改成
    document.getElementById("username").value ==""试试或者把<form name="form1" action="/noframe/AddServlet" method="post" > 
    改成<form name="form1" action="/noframe/AddServlet" method="post" onsubmit="submitForm()">
    然后下面的onclick去掉
      

  2.   


    <script type="text/javascript"> 
      function submitForm(){ 
        if(document.getElementById("username").value==""){ 
            alert("请输入姓名"); 
            document.form1.username.focus(); 
            return false; 
        }else if(document.getElementById("password").value==""){ 
            alert("请输入密码"); 
            document.form1.password.focus(); 
            return false; 
        }else{ 
            return true; 
        } 
      } 
    </script> 
      </head> 
      
      <body> 
        <center> <h2>增加人员信息 </h2> <br> 
        <form name="form1" action="/noframe/AddServlet" method="post" > 
        人员姓名: <input type="text" id="username" name="username"> 
        登录密码: <input type="password" id="password" name="password"> <br> 
      <input type="submit" name="提交" value="提交" onclick="submitForm()">这里好像不执行页面直接跳转 
      <input type="reset"  name="重置" value="清空"> 
        </form> 
        </center> 
      </body> 
    </html> try,你input没定义ID当然不行了。。
      

  3.   

    document.form1.username.value=""
    document.form1.password.value=""
    “=”都改成“==”onclick="submitForm()"改成onclick="return submitForm()"