简体   繁体   English

JavaScript函数中的While循环

[英]While loop within Javascript function

I have the following basic function: 我具有以下基本功能:

<script type="text/javascript">
function Form_Data(theForm)
{

var t=1;
while (t<=5) {

if (theForm.F[t]FirstName.value == "")
    {
    alert("Please enter Fighter 1's First Name.");
    theForm.F[t]FirstName.focus();
    return (false);
    }
t++;
}

return (true);
}
</script>

The script (js validation) fails using this code. 脚本(js验证)无法使用此代码。 If I remove the [t] and replace with a number (1,2,3,4,etc), the validation works on the appropriate fields. 如果删除[t]并替换为数字(1、2、3、4等),则验证将在相应的字段上进行。 What am I doing wrong? 我究竟做错了什么?

您不能将索引用作名称的一部分,而必须将名称作为字符串放置在一起并用作索引:

theForm['F' + t + 'FirstName']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM