首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

输入框的检测有关问题,也许有点难,请

2012-03-18 
输入框的检测问题,也许有点难,,请高手指点.有50多个输入框,而输入框的name就是后面的数字不同,如:inputna

输入框的检测问题,也许有点难,,请高手指点.
有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
*/
function checkAllNecessaryInputs(formObj)


{
if(formObj==null) formObj=document.forms[0];
var theFirstNecessaryInputToBeFilled=null;//Get it to focus;
var theAlertStr= " ";
var theNumOfInputsToBeFilled=0;
var theElementsOfTheForm=formObj.elements;
for (var i=0;i <theElementsOfTheForm.length;i++)
{
if(theNumOfInputsToBeFilled> 0) break;
if((theElementsOfTheForm[i].altStr!=null)
&&(theElementsOfTheForm[i].altStr!= " ")
&&(trim(theElementsOfTheForm[i].value)== " ")
)
{
try{theElementsOfTheForm[i].focus();}catch(e){continue;}
theNumOfInputsToBeFilled++;
theAlertStr=theAlertStr+ " "+theElementsOfTheForm[i].altStr;
if(theFirstNecessaryInputToBeFilled==null)
theFirstNecessaryInputToBeFilled=theElementsOfTheForm[i];
}
}
if(theNumOfInputsToBeFilled> 0)
{
alert( "请输入: "+theAlertStr);
theFirstNecessaryInputToBeFilled.focus();
return false;
}
return true;
}
[解决办法]
<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>

热点排行
Bad Request.