Html 表单元素数组-分组单选按钮

Html 表单元素数组-分组单选按钮,html,Html,“我的web表单”包含一行,其中包含以下文本和单选元素: <input type="text" name="name" value=""> <input type="radio" name="gender" value="M"> M <input type="radio" name="gender" value="F"> F M F 现在我想用多个这样的行创建这个表单,所以我需要使用表单元素数组。对于文本元素,我将使用: <inpu

“我的web表单”包含一行,其中包含以下文本和单选元素:

  <input type="text" name="name" value="">
  <input type="radio" name="gender" value="M"> M
  <input type="radio" name="gender" value="F"> F

M
F
现在我想用多个这样的行创建这个表单,所以我需要使用表单元素数组。对于文本元素,我将使用:

  <input type="text" name="name[]" value="">

但是如何处理无线电元素(它们已经是一个阵列)。我不能就这么做

  <input type="radio" name="gender[]" value="M"> M
  <input type="radio" name="gender[]" value="F"> F
M
F
因为第二个元素的索引将增加,这将从同一组中分离两个单选按钮。我也不能这样做:

  <input type="radio" name="gender[][]" value="M"> M
  <input type="radio" name="gender[][]" value="F"> F
M
F

那么,该怎么办呢?

您仍然可以使用数组,如下所示:

<!-- First Group -->
<input type="radio" name="gender[0]" value="M"> M
<input type="radio" name="gender[0]" value="F"> F

<!-- Second Group -->
<input type="radio" name="gender[1]" value="M"> M
<input type="radio" name="gender[1]" value="F"> F

<!-- Third Group -->
<input type="radio" name="gender[2]" value="M"> M
<input type="radio" name="gender[2]" value="F"> F

M
F
M
F
M
F

您只需跟踪数组的索引。

好的,没错,但我在foreach循环中创建了行,所以没有显式的增量。如果没有其他解决方案,我将介绍它。我认为有一个更简单的解决方案,使用隐式incrementor,比如名称[]。服务器端代码使用什么语言?你能在问题中发布你的代码吗?我使用PHP,但这有什么关系?发布这段代码的目的是什么(这只是常规的foreach(){}循环,在这里我将引入$I++incrementor来索引表单元素)。