日期:2014-05-17  浏览次数:21044 次

输入框的检测问题,也许有点难,,请高手指点
有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= " ">
------解决方案--------------------
循环i,不可出错哦!
document.product[i].value == " "


-------------------------
www.jw112.com
------解决方案--------------------
document.all.product[i].value == " "

------解决方案--------------------
<form action=11.asp method=post onsubmit= "return ss(); ">
<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= "product5 " type= "text " value= " ">
<input value= "提交 " type=submit>
</form>
<script>
function ss()