Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中,是否有方法在for循环的每次迭代中捕获变量的值?_Javascript_Html_Variables_Loops_For Loop - Fatal编程技术网

在javascript中,是否有方法在for循环的每次迭代中捕获变量的值?

在javascript中,是否有方法在for循环的每次迭代中捕获变量的值?,javascript,html,variables,loops,for-loop,Javascript,Html,Variables,Loops,For Loop,我是一名学习javascript的学生,现在才3周,所以我不熟悉任何高级主题或代码。这是一个分级作业,所以我不寻找答案,但任何帮助或建议,以完成这将是伟大的。我已经被困了几个小时,我真的不知道该往哪个方向走 <body> <h1>Assignment 2</h1> <p> Enter First Name: <input id="firstname" type="text"> <span id="f

我是一名学习javascript的学生,现在才3周,所以我不熟悉任何高级主题或代码。这是一个分级作业,所以我不寻找答案,但任何帮助或建议,以完成这将是伟大的。我已经被困了几个小时,我真的不知道该往哪个方向走

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
我需要做的是使用for循环获取最多3个不同id标记的值,用户可以在html页面中输入这些标记。它必须在for循环中完成,并使用计数器顺序更改id的名称

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
我能够做到这一点,但是每次循环运行时,它都会获取数据,当然,还会将变量设置为循环的最新迭代

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
是否有办法捕获每个循环中的返回值,并将其存储起来,以便在循环运行后使用?我应该能够生成3个单独的值,然后一起显示它们

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
编辑:这是HTML:

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>

任务2

输入名字:
*

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
输入姓氏: *

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
你有多少宠物?(0-3): *

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
列出您宠物的名字:

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>

*

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>

您可以使用单独的变量累积存储每次迭代的值。确保在循环之前定义此变量,并在每次迭代时将数据添加到变量中,而不是覆盖它

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
例如,使用
字符串
变量:

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
var str = '';
for (i=0; i < 3; i++) {
    str += i+' '; // Note the usage of the += operator to append the value
}
 //
console.log(str);
var arr = [];
for (i=0; i < 3; i++) {
    for (i=0; i < 3; i++) {
    arr.push(i);
}
 //
console.log(arr.toString());

请注意,出于示例的考虑,我们只是存储循环计数器的值,
i

您可以使用单独的变量累积存储每次迭代的值。确保在循环之前定义此变量,并在每次迭代时将数据添加到变量中,而不是覆盖它

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
例如,使用
字符串
变量:

<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>
var str = '';
for (i=0; i < 3; i++) {
    str += i+' '; // Note the usage of the += operator to append the value
}
 //
console.log(str);
var arr = [];
for (i=0; i < 3; i++) {
    for (i=0; i < 3; i++) {
    arr.push(i);
}
 //
console.log(arr.toString());

注意,为了便于示例,我们只存储循环计数器的值,
i

取决于HTML。请发布一个代码段。您可以将每次迭代的值存储在另一个变量(如字符串或数组)中。感谢您的快速回复。我还没有了解数组,但是当我将它存储为字符串时,每次循环运行时它都会被覆盖。因此,我成功地获取了值,但如果没有在下一个循环中写入,就无法存储它。我如何避免这种情况?我的教授提供了这个。作业2输入名字:

输入姓氏:

你有多少宠物?(0-3):*

列出您宠物的名字:

*

如果要使用字符串,请首先创建一个空字符串:
var result=''
然后附加结果,如
result+=“要添加的字符串片段”
(这意味着:
result=result+“要添加的字符串”;
)。如果要使用唯一的分隔字符,还可以在同一个字符上拆分它(到一个数组)。但是,使用数组可能更好(而且是一个非常有用的概念!)!取决于HTML。请发布一个代码段。您可以将每次迭代的值存储在另一个变量(如字符串或数组)中。感谢您的快速回复。我还没有了解数组,但是当我将它存储为字符串时,每次循环运行时它都会被覆盖。因此,我成功地获取了值,但如果没有在下一个循环中写入,就无法存储它。我如何避免这种情况?我的教授提供了这个。作业2输入名字:

输入姓氏:

你有多少宠物?(0-3):*

列出您宠物的名字:

*

如果要使用字符串,请首先创建一个空字符串:
var result=''
然后附加结果,如
result+=“要添加的字符串片段”
(这意味着:
result=result+“要添加的字符串”;
)。如果要使用唯一的分隔字符,还可以在同一个字符上拆分它(到一个数组)。但是,使用数组可能更好(而且是一个非常有用的概念!)!谢谢我会试试看,看起来这就是我想做的@用户2780236没问题。根据您的上述评论,更新答案,并提供更多相关信息。谢谢!我会试试看,看起来这就是我想做的@用户2780236没问题。根据您的上述评论,更新了答案,并提供了更多相关信息。
<body>
  <h1>Assignment 2</h1>
  <p>
    Enter First Name:
    <input id="firstname" type="text">
    <span id="firstname_error">*</span>
  </p>
  <p>
    Enter Last Name:
    <input id="lastname" type="text">
    <span id="lastname_error">*</span>
  </p>
  <p>
    How Many Pets do you have? (0-3):
    <input id="numpets" maxlength="1" size="1" type="text">
    <span id="numpets_error">*</span>
  </p>
  <p>
    List your Pet's names:
    <input id="pet1" type="text">
    <input id="pet2" type="text">
    <input id="pet3" type="text">
  </p>
  <p>
     <input onclick="processInfo()" type="button" value="Submit Information">
  </p>
  <p id="message">*</p>
</body>
</html>