我在此站点上看到了一些东西: 用JavaScript和PHP处理HTML表单元素数组 http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343
它说过将数组放在name属性中,以及如何获取输入集合的值。例如name="education[]"
name="education[]"
但据我所知,HTML input元素已通过数组就绪name。例如,在客户端(GetElementsByName)或服务器端($_POST在PHP或Request.FormASP.NET中):,或没有name="education",有什么区别[]?
name
GetElementsByName
$_POST
Request.Form
name="education"
[]
PHP使用方括号语法将表单输入转换为数组,因此使用name="education[]"时将获得一个数组:
$educationValues = $_POST['education']; // Returns an array print_r($educationValues); // Shows you all the values in the array
因此,例如:
<p><label>Please enter your most recent education<br> <input type="text" name="education[]"> </p> <p><label>Please enter any previous education<br> <input type="text" name="education[]"> </p> <p><label>Please enter any previous education<br> <input type="text" name="education[]"> </p>
将为您提供$_POST['education']数组内所有输入的值。
$_POST['education']
在JavaScript中,通过id获取元素更有效。
document.getElementById("education1");
ID不必与名称匹配:
<p><label>Please enter your most recent education<br> <input type="text" name="education[]" id="education1"> </p>