<script>
function checks(k)
{
for(var i=1;i<=k;i++){
var tt="result"+i+"";
if (document.form1.tt.value==""){
alert("请选择核实的结果!");
document.form1.tt.focus();
return false;
}
}
return true;
}
</script> <script>
function checks(k)
{
for(var i=1;i<=k;i++){
var tt="result"+i+"";
if (document.form1.result1.value==""){
alert("请选择核实的结果!");
document.form1.result1.focus();
return false;
}
if (document.form1.result2.value==""){
alert("请选择核实的结果!");
document.form1.result2.focus();
return false;
}
}
return true;
}
</script> 为什么第一个JS执行有错误,第二个却可以执行?
function checks(k)
{
for(var i=1;i<=k;i++){
var tt="result"+i+"";
if (document.form1.tt.value==""){
alert("请选择核实的结果!");
document.form1.tt.focus();
return false;
}
}
return true;
}
</script> <script>
function checks(k)
{
for(var i=1;i<=k;i++){
var tt="result"+i+"";
if (document.form1.result1.value==""){
alert("请选择核实的结果!");
document.form1.result1.focus();
return false;
}
if (document.form1.result2.value==""){
alert("请选择核实的结果!");
document.form1.result2.focus();
return false;
}
}
return true;
}
</script> 为什么第一个JS执行有错误,第二个却可以执行?
第一个JS里的
var tt="result"+i+"";<<<-------------此tt很显然是字符串
if (document.form1.tt.value=="")<<<-系统不认识form1里的字串tt是谁,
只认识form1下的对象tt,但不存在。
==================================================================================================
2、根治:
for(var i=1;i<=k;i++){
var o=docuemnt.getElementById("result"+i);
if (o.value.length==0){
alert("请选择核实的结果!");
document.form1.tt.focus();
return false;
}
}
2、根治:var o=null;
for(var i=1;i<=k;i++){
o=docuemnt.getElementById("result"+i);
if (o.value.length==0){ alert("请选择核实的结果!"); o.focus(); return false; }
}
这里tt个字符串对象
document.form1.tt.value
这里tt是form1的一个属性,不是同一个东西解决方法:
document.form1[tt].value
这样就可以了
JS点语法并不适用于用字符串表示的属性和方法。改成这样试试eval("document.form1."+ tt +".value")另外:请使用 W3C 的标准形式 document.getElementById() 。
这样会有性能问题的
另外这个document.form1.XX.value
你确定不是W3C标准吗?
但是alert不出来,也就是说验证没有成功
function checks(k)
{
var o=null;
for(var i=1;i<=k;i++){
o=docuemnt.getElementById("result"+i);
if (o.value.length==0){
alert("请选择核实的结果!");
o.focus();
return false;
}
}
return true;
}
</script>
<form name="form1" method="post" action="" onSubmit="return checks(2)">
<table width="350" border="1" cellpadding="0" cellspacing="0" bordercolor="F7F7F7">
<tr bgColor=#e0e6ed>
<td height="30" colspan="3"><div align="center">人口信息审核</div></td>
</tr>
<tr>
<td height="30">提交事项</td>
<td height="30">提交值</td>
<td height="30">核实结果</td>
</tr>
<tr bgcolor="#FFFFFF" >
<td height="30">身份证号</td>
<td height="30">312</td>
<td height="30">
<select name="result1" id="result1">
<option value="">选择审核结果</option>
<option value="1" >属实</option>
<option value="2" >不属实</option>
</select>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="30">文化程度
</td>
<td height="30">312</td>
<td height="30">
<select name="result2" id="result2">
<option value="">选择审核结果</option>
<option value="1" >属实</option>
<option value="2" >不属实</option>
</select>
</td>
</tr>
</table>
<TABLE width="350" border=0 align="left" cellPadding=5 cellSpacing=0 >
<TBODY>
<TR height="10" bgColor=#FFFFFF> <TD width="100%" colspan="2" ></TD>
</TR>
<TR height="30" bgColor=#e0e6ed> <TD width="33%" align="right"><input name="t" type="hidden" id="t" value="2">
<INPUT name="提交" type="submit" value="确定" class="button1"></TD>
<TD width="50%" align="left"> <INPUT class="button1" type="button" value="取消" onClick="javascript:window.close();"></TD>
</TR>
</TBODY>
</TABLE>
</form>
请检查你的代码里 "result"+i代表的是id还是name;如果是id可以用2楼代码;如果是name,不可以用2楼代码。如果是你的代码里 "result"+i代表的是name,建议放弃这种方式传递数据。
例如,如果是下列:
<form method=post action="aaa.asp" name=form1>
<input type=text name="result1" value="" />
<input type=text name="result2" value="" />
<input type=text name="result3" value="" />
<input type=text name="result4" value="" />
<input type=submit name="submit" value="提交" />
</form>建议改为:
<script>
function checks(k){
var o=document.getElementsByName("result")'得到"result"文本框数组
if (o[0].value.length==0){alert("请选择核实的结果!");o[0].focus(); return false; }
if (o[1].value.length==0){alert("请选择???的结果!");o[1].focus(); return false; }
//.........................
}
</script> <form method=post action="aaa.asp" name=form1 onsubmit="checks()">
%>
for i=0 to 3
response.write("<input type=text name='result' value='' />")
next
<%
<input type=submit name="submit" value="提交" />
</form>
但是input的行数是动态的生成的
==================================================
<script>
function checks(k){
var o=null;
for(var i=1;i<=k;i++){
o=docuemnt.getElementsByName("result"+i)[0];
if (o.value=="0"){ alert("请选择核实的结果!"); o.focus(); return false; }
}
}
</script>
<select name="result1" id="result1">
<option value="0">选择审核结果</option>
<option value="1" >属实</option>
<option value="2" >不属实</option>
</select>
</td>
</tr>
<tr bgcolor="#FFFFFF">
<td height="30">文化程度
</td> <td height="30">312</td>
<td height="30">
<select name="result2" id="result2">
<option value="0">选择审核结果</option>
<option value="1" >属实</option>
<option value="2" >不属实</option>
</select>
<script>
function checks(k)
{
for(var i=1;i<=k;i++){
var tt="result"+i+"";
if (document.form1[tt].value==""){
alert("请选择核实的结果!");
document.form1[tt].focus();
return false;
}
return true;
}
</script>
12楼还是无法正确验证,13楼正解但是form1[tt],这个[]起的是什么作用呢?我记得数组才用[]啊