用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">
用 户 名:
</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">
教 师 号:
</td>
<td width="50%" align="left">
<input type="text" name="tno"/>
</td>
</tr>
<tr align="center">
<td width="50%" align="right">
性 别:
</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">
生 日:
</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>
现付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">
用 户 名:
</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">
教 师 号:
</td>
<td width="50%" align="left">
<input type="text" name="tno"/>
</td>
</tr>
<tr align="center">
<td width="50%" align="right">
性 别:
</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">
生 日:
</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>
src 后面路径应该用<%=basePath%>加绝对路径。
把basepath 加到前面代码里