我现在的问题是,在遍历的时候是对一个表单循环遍历的,表单名没变,所以在修改验证的时候,修改第一个可以,但剩下的不行。请教各位js高手。
代码如下
</style>
<script type="text/javascript">
function check(){
var x=document.form1.studentAge.value;
var y=parseInt(x);
if(document.form1.studentName.value==""){
document.form1.studentName.focus();
alert("对不起,请填写学生姓名");
return false;
}
if(document.form1.studentAge.value==""){
document.form1.studentAge.focus();
alert("对不起,请填写学生年龄");
return false;
}
if(document.form1.studentSex.value==""){
document.form1.studentSex.focus();
alert("对不起,请填写学生性别");
return false;
}
if(isNaN(y)){
alert("对不起,请填写数字");
return false;
}
if(y<5||y>80){
alert("对不起,请填写正确年龄5到80");
return false;
}
if(document.form1.studentSex.value!="男"&&document.form1.studentSex.value!="女"){
document.form1.studentSex.focus();
alert("对不起,请正确填写学生性别");
return false;
}
}
</script>
</head> <body>
<div align="center">
<table width="530" border="1">
<tr>
<th width="117" scope="col">
学生姓名
</th>
<th width="135" scope="col">
学生年龄
</th>
<th width="131" scope="col">
学生性别
</th>
<th width="119" scope="col">
操作
</th>
</tr>
</table>

<%
for (int i = 0; i < list1.size(); i++) {
StudentBean studentBean = (StudentBean) list1.get(i);
int studentId = studentBean.getStudentId();
String studentName = studentBean.getStudentName();
int studentAge = studentBean.getStudentAge();
String studentSex = studentBean.getStudentSex();
%>

<table width="530" border="1">
<form id="form1" name="form1" method="post" action="StudentServlet?page=updateStudent">
<tr>
<th width="117" scope="col">
<input name="studentName" type="text" size="14"
value="<%=studentName%>" />
</th>
<th width="135" scope="col">
<input name="studentAge" type="text" size="15"
value="<%=studentAge%>" />
</th>
<th width="131" scope="col">
<input name="studentSex" type="text" size="15"
value="<%=studentSex%>" />
<input name="studentId" type="hidden" size="15"value="<%=studentId%>" />
</th>
<th width="119" scope="col">
<label>
<a href="StudentServlet?page=delStudent&action=<%=studentId%>">删除</a>
&nbsp;
<input onclick="return check();" type="submit" name="Submit2" value="修改">
</label>
</th>
</tr>
</form>
</table>
<%
}
%>
<p align="center">
<a href="StudentAdd.jsp">添加</a>
</p>

</div>
</body>
</html>

解决方案 »

  1.   


    <script type="text/javascript">
    function check(){
      with (document.form1){
        for (var i=0;i<studentName.length;i++){
          if(studentName[i].value==""){studentName[i].focus();  alert("对不起,请填写学生姓名");  return false;}
          if(studentAge[i].value==""){studentAge[i].focus();  alert("对不起,请填写学生年龄");  return false;}
          if(!parseInt(studentAge[i].value)){studentAge[i].focus();  alert("对不起,请填写数字");  return false;}
          if(studentAge.value[i]*1)<5||studentAge.value[i]*1)>80){studentAge[i].focus();  alert("对不起,请填写正确年龄5到80");  return false;}
          if(studentSex[i].value==""){studentSex[i].focus();  alert("对不起,请填写学生性别");  return false;}
          if(studentSex[i].value!="男"||studentSex[i].value!="女"){studentSex[i].focus();  alert("对不起,请正确填写学生性别");  return false;}
        }
      }
    }
    </script>