<form name=frm>
a:<input type="text" alt=a>*<br>
b:<input type="text"><br>
c:<input type="text" alt=c>*<br>
d:<input type="text"><br>
e:<input type="text" alt=e>*<br>
f:<input type="text"><br>
g:<input type="text" alt=g>*<br>
h:<select alt=h><option value=""><option>1</select>*<br>
i:<select><option value=""><option>1</select><br><input type=button value="完成" onclick="checkNecessaryInput(frm)">
</form><script>
function checkNecessaryInput(obj) //用于检查一个表单内所有必须输入的栏位是否输入完整
{
var s="";
var i;
var j=0;
for (i=0 ;(i<obj.elements.length)&&(j<10);i++)
{
if (( obj.elements[i].alt != null ) && (obj.elements[i].alt.length>0) && (obj.elements[i].value!=null) && (obj.elements[i].value.length==0) )
{
s+="\n"+obj.elements[i].alt; //遍历所有输入,如果必须输入且没有输入值,就将其内容加到警告信息中去
j=j+1;
}
}
if (j > 0 )
{
alert ("以下资料必须输入"+s);
return false;
}
return true;
}</script>

解决方案 »

  1.   

    如果是在div里面的话,owner就是div。如果是在document里面的话,owner就是documentfunction check(){
    inputs=owner.getElementsByTagName("INPUT");
    for(i=0;i<inputs.length;i++){
    if(inputs[i].type=="text" && inputs[i].value=="")  alert("null");
    }}
      

  2.   

    是这样?
    <html>
    <head>
    </head>
    <body>
    <input type="text" >
    <input type="text" >
    <input type="text" >
    <input type="text" >
    <input type="button"    onclick="check()">
    <script Language="Javascrpt">
    function check(){
    inputs=document.all.getElementsByTagName("INPUT");
    for(i=0;i<inputs.length;i++){
    if(inputs[i].type=="text" && inputs[i].value=="")  alert("null");
    }
    }
    </script>
    </body>
    </html>
      

  3.   

    <html>
    <head>
    </head>
    <body>
    <input type="text" >
    <input type="text" >
    <input type="text" >
    <input type="text" >
    <input type="button"    onclick="check()">
    <script Language="Javascript">
    function check(){
    inputs=document.getElementsByTagName("INPUT");
    for(i=0;i<inputs.length;i++){
    if(inputs[i].type=="text" && inputs[i].value=="")  alert(i+" is null");
    }
    }
    </script>
    </body>
    </html>