我想用一个js文件验证表单,使其在提交时不为空。可是最后为什么不起效果呢,请各位看一下是我写错了还是配错了 谢谢哈
success.jsp(在WebRoot目录下)的表单相关代码如下:
<head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'success1.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">

<style type="text/css">
<!--
a:link {font-family:"宋体";text-decoration:none}
a:visited {font-family:"宋体";text-decoration:none}
a:hover {font-family:"宋体";text-decoration:none}
-->
</style>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript"  language="javascript" src="js/checkForm.js"></></script>
  </head>
  </head>
  
  <body >
  <center>
  <form name="login" action="<%= request.getContextPath()%>/CheckLoginServlet" method="post" >
  <table width="80%" border="1">
  <tr>
     <td align="center" colspan="2"><img src="images/regsucc.PNG" width="216" height="44" /></td>
  </tr>
  <tr>
   <td width="50%"><div align="right">用户名:</div></td>
    <td width="50%"><input type="text" name="username" /></td>
  </tr>
  <tr>
     <td><div align="right">密码:</div></td>
    <td><input type="password" name="password" /></td>
  </tr>
  <tr>
    <td align="right"><input type="submit" name="login" value="登录" onclick="checkForm(login)"/></td>
    <td ><div align="left"><a href="register.jsp"><img src="images/rereg.PNG" width="90" height="32" border="0"/></a></div></td>
  </tr>
</table></form>js文件在WebRoot目录下的js文件夹里面,checkForm.js的代码如下
function checkForm(form){
if(isEmpty(form.username.value) || isEmpty(form.password.value)){
alert("请把登录信息填写完整!");
return false;
}
}

解决方案 »

  1.   

    <input type="submit" name="login" value="登录" onclick="checkForm(login)"/>
    你的方法放错了!
    你的type是submit、一点击它就提交表单了!
    应该在<form>标签中加入onsubmit="return checkForm(login)"方法!
    然后在checkForm(login)方法内部给一个返回值true或者false。
    这样、当方法返回false的时候、表单就不会提交了!
     
      

  2.   

    src="${pageContext.request.contextPath }/js/checkForm.js"
      

  3.   

    input type="submit" name="login" value="登录" onclick="checkForm($login)"/></