用eclipse建立的WEB工程,部署在tomcat上,我想用javascript验证表单,如果前后密码输入不一致,则要求用户重新填写。 如若把javascript代码直接写在jsp页面内,则正常执行,但是把他们封装到单独的*.js文件的话,则不正常执行,只是在浏览器的状态栏里显示页面有错误产生。我检查了JS文件的包含路径,没有错误,更奇怪的是,如果我用html文件代替JSP文件用浏览器访问,用同样的JS文件则没有错误,可以正常执行,很纳闷。
 
现付JS代码和JSP页面代码在附件,请老师帮我看下。
=====下面我写在js/checkForm.js中======
function checkPass() {
 var password = document.getElementById("password");
 var repassword = document.getElementById("repassword");
 
 if (password.value != repassword.value) {
  password.value = "";
  repassword.value = "";
  password.focus();
  window.alert("password is not right");
  return false;
 }
 return true;
 
============下面是JSP代码==========
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
 <title>teacher_reg.jsp</title>
 <script type="text/javascript" src="js/checkForm.js">
 </script>
 
 
</head>
<body>
 <%@include file="header.jsp"%><br>
  <html:form action="/student_reg.do" method="post" onsubmit="return check(this);">
   <table align="center" width="100%" height="60%" background="bg.jpg">
    <tr align="center">
     <td width="50%" align="right">
      用&nbsp;户&nbsp;名:
     </td>
     <td width="50%" align="left">
      <input type="text" name="userName"/>
     </td>
    </tr>
     
    <tr align="center">
     <td width="50%" align="right">
      真实姓名:
     </td>
     <td width="50%" align="left">
      <input type="text" name="name"/>
     </td>
    </tr>
    <tr align="center">
     <td width="50%" align="right">
      用户密码:
     </td>
     <td width="50%" align="left">
      <input id="password" type="password" name="password"/>
     </td>
    </tr>
    <tr align="center">
     <td width="50%" align="right">
      密码验证:
     </td>
     <td width="50%" align="left">
      <input id="repassword" type="password" name="repassword" onblur="return checkPass();"/>
     </td>
    </tr>
    <tr align="center">
     <td width="50%" align="right">
      教&nbsp;师&nbsp;号:
     </td>
     <td width="50%" align="left">
      <input type="text" name="tno"/>
     </td>
    </tr>
    <tr align="center">
     <td width="50%" align="right">
      性&nbsp;&nbsp;&nbsp;&nbsp;别:
     </td>
     <td width="50%" align="left">
      男
      <input type="radio" name="gender" value=true checked />
      女
      <input type="radio" name="gender" value=false />
     </td>
    </tr>
    
     <tr align="center">
     <td width="50%" align="right">
      生&nbsp;&nbsp;&nbsp;&nbsp;日:
     </td>
     <td width="50%" align="left">
      <select name="year">
      <% for(int i=1980; i<=1990; i++){ %>
       <option><%=i%></option>
      <%} %>
      </select>
      年
      <select name="month">
      <% for(int i=1; i<=12; i++){ %>
       <option><%=i%></option>
      <%} %>
      </select>
      月
      <select name="day">
       <% for(int i=1; i<=31; i++){ %>
       <option><%=i%></option>
       <%}%>
      </select>
      日       
     </td>
    </tr>
   </table>
   <hr>
    <center>
     <input type="submit" value="提交" />
     <input type="button" value="重填" />
    </center>
      
  </html:form>
 <%@include file="tail.jsp" %>
</body>
</html:html>

解决方案 »

  1.   

    每种文档的都有相关的文档类型定义,也就是规定了文档的元素,和元素的先后顺序,如果你很清楚文档的元素,你可以自己手写,借助开发工具,它可以将你的元素,放到正确的位置,比方说,你有一个js外部文件和自己在页面写的js function ,那么自己写的function 一定要在 外部应用js 文件之前
      

  2.   

    <script type="text/javascript" src="js/checkForm.js">
    src 后面路径应该用<%=basePath%>加绝对路径。
    把basepath 加到前面代码里