日期:2014-05-16  浏览次数:20443 次

输入框的检测问题,真的有点难,请高手指点.

<script   language= "JavaScript ">
function   bb(){
var   b=document.getElementsByTagName( "input ")
for(var   i=0;i <b.length;i++){
if(b[i].name.substr(0,7)== "product "){
if(b[i].value== " "){
alert( "请产品 "+i+ "的值。 ");
//exit   for
//b[i].name.focus();
return   (false);
}
}
}
}
</script>

<form   name= "form1 "   action= "input.asp "   onSubmit= "bb() ">
<input   name= "product1 "   type= "text "   value= " "   >
<input   name= "product2 "   type= "text "   value= " ">
<input   name= "product3 "   type= "text "   value= " ">
<input   name= "product4 "   type= "text "   value= " ">
<input   name= "rr "   type= "submit "   value= "OK ">
</form>
看看这里
alert( "请产品 "+i+ "的值。 ");
                                    //exit   for
//b[i].name.focus();   注意这一行
return   (false);
如何做到让alert确定后,退出FOR,且焦点落在product[i]中,并且不能提交?

------解决方案--------------------
<html>
<head>
<title>
dfjk </title>
</head>
<body>


<script language= "JavaScript ">
function bb(){
var b=document.getElementsByTagName( "input ")
for(var i=0;i <b.length;i++){
//这要加上 if(b[i].name.length> 7){这个判断,因为别的input的name可能没7个,.substr(0,7)的时候会出错
if(b[i].name.substr(0,7)== "product "){
if(b[i].value== " "){
alert( "请产品 "+(i+1)+ "的值。 ");
//exit for
b[i].focus();
return false;
}
}
}
return true
}
</script>

<form name= "form1 " action= "input.asp " onSubmit= "return bb() ">
<input name= "product1 " type= "text " value= " " >
<input name= "product2 " type= "text " value= " ">
<input name= "product3 " type= "text " value= " ">
<input name= "product4 " type= "text " value= " ">
<input name= "rr " type= "submit " value= "OK ">
</form>
</body>
</html>