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

输入框的检测问题,也许有点难,,请高手指点.
有50多个输入框,而输入框的name就是后面的数字不同,如:
<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= "product55 "   type= "text "   value= " ">
<input   name= "product56 "   type= "text "   value= " ">

而我想实现用JAVASCRIPT检测每个输入框是否为空,
传统做法是一个个地写代码:
    if   (theForm.product1.value   ==   " "   )
    {
        alert( "请填写产品1。 ");
        theForm.product1.focus();
        return   (false);
    }
    if   (theForm.product2.value   ==   " "   )
    {
        alert( "请填写产品2。 ");
        theForm.product2.focus();
        return   (false);
    }
……
可是我想实现不用一个个地写,,有没有什么数组或参数,,写一个,然后用JAVASCRIPT循环检测。
    if   (theForm.product[i].value   ==   " "   )
    {
        alert( "请填写产品 "+[i]+ "。 ");
        theForm.product[i].focus();
        return   (false);
    }
所以想请教高手,这个代码怎么写呀??


------解决方案--------------------
<script>

function aa(){
var b=document.getElementsByTagName( "input ")
for(var i=0;i <b.length;i++){
if(b[i].type== "text "){
if(b[i].name.length> 7){
if(b[i].name.substr(0,7)== "product "){
alert(b[i].name)
alert(b[i].value)
}
}
}
}
}
</script>
<input name= "product1 " type= "text " value= " " onclick= "aa() ">
<input name= "product2 " type= "text " value= " ">
<input name= "product3 " type= "text " value= " ">
<input name= "product4 " type= "text " value= " ">
------解决方案--------------------
lz最好还要考虑null的情况。
------解决方案--------------------
<input name= "product1 " altStr= "产品1 " type= "text " value= " ">
<input name= "product2 " altStr= "产品2 " type= "text " value= " ">
<input name= "product3 " altStr= "产品3 " type= "text " value= " ">
<input name= "product4 " altStr= "产品4 " type= "text " value= " ">


/**
* This function is to get if all necessary inputs have been inputted.
* Please give the NecessaryInput a property named "altStr ".This property will be alert when it has not been inputed.
* For Example,
* This is a necessary input: <input altStr= "Name ">
* This is not a necessary input: <input >
* JK 2003-12-08
*/
functi