如何使用循环for javascript [checkform function]?
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
if (form.text_0.value == "") {
alert( "Please enter text" );
return false ;
}
return true ;
}
</script>
我尝试像这样使用循环
<script language="JavaScript" type="text/javascript">
function checkform ( form )
{
var y;
for (y = 0; y < 10; y++) {
if (form.text_'y'.value == "") {
alert( "Please enter text" );
return false ;
}
}
return true ;
}
</script>
但不行,我该怎么做?
答案 0 :(得分:1)
您可以使用[]
运算符:
if (form["text_" + y].value == "") {