我在这个网站上看到了一些东西:
处理JavaScript和 http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=343中的HTML元素数组
它说要将数组放在name属性中,以及如何获取输入集合的值。例如,name="education[]"
但是,正如我所知道的,HTML输入元素是由name准备好的。在客户端(GetElementsByName)或服务器端( PHP中的$_POST或ASP.NET中的Request.Form )。
例如:name="education",那么有无[]有什么不同?
发布于 2011-01-14 08:29:35
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']数组中输入的所有值。
在JavaScript中,通过id获得元素的效率更高。
document.getElementById("education1");该id不必与名称匹配:
<p><label>Please enter your most recent education<br>
<input type="text" name="education[]" id="education1">
</p>发布于 2011-01-14 07:39:44
如果有复选框,则可以传递一个选中值数组。
<input type="checkbox" name="fruits[]" value="orange"/>
<input type="checkbox" name="fruits[]" value="apple"/>
<input type="checkbox" name="fruits[]" value="banana"/>也有多重选择下拉列表
<select name="fruits[]" multiple>
<option>apple</option>
<option>orange</option>
<option>pear</option>
</select>发布于 2017-03-23 07:40:25
这是不同的。
如果你张贴这份表格:
<input type="text" name="education[]" value="1">
<input type="text" name="education[]" value="2">
<input type="text" name="education[]" value="3">您将在PHP中得到一个数组。在本例中,您将得到$_POST['education'] = [1, 2, 3]。
如果您在没有[]的情况下发布此表单,
<input type="text" name="education" value="1">
<input type="text" name="education" value="2">
<input type="text" name="education" value="3">你会得到最后的价值。在这里,您将得到$_POST['education'] = 3。
https://stackoverflow.com/questions/4688880
复制相似问题