你甚至不需要 JS 来编写“更清晰的代码”。您可以通过这种方式将数据数组从表单传递给 PHP。这甚至已经足够清楚了。取自我的另一个答案,您可以这样做(上一个问题是关于复选框,但应该适用于任何输入类型):
<input type="checkbox" name="answers[set1][]" value="apple" /> //imagine checked
<input type="checkbox" name="answers[set1][]" value="orange" /> //imagine checked
<input type="checkbox" name="answers[set1][]" value="grape" />
<input type="checkbox" name="answers[set2][]" value="airplane" /> //imagine checked
<input type="checkbox" name="answers[set2][]" value="train" /> //imagine checked
<input type="checkbox" name="answers[set2][]" value="boat" />
<input type="checkbox" name="answers[solo]" value="boar" /> //single type value. note that there is no [] in the end
在请求数组中像这样结束(比如 POST):
$_POST[] = array(
'answers' => array(
'set1' => array('apple','orange'), //unchecked items won't be included
'set2' => array('airplane','train'), //unchecked items won't be included
'solo' => 'boar'
)
);