如果我有这样的表格:
<form>
<input name="param[]" />
<input name="param[]" />
<input name="param[]" />
</form>
我能否期望在提交表单时以相同的顺序接收这些参数?
我注意到 Chrome、Rails 和 Webrat 保留了参数的顺序,但这可能是由实现细节而不是遵守标准引起的。
最佳答案
这是 standard .我发现 W3C specification非常有用和可读。
application/x-www-form-urlencoded...
The control names/values are listed in the order they appear in the document.
multipart/form-data...
The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream.
我还没有遇到不遵循这个约定的浏览器,所以我想说你可以很安全地使用它。
话虽这么说,我的语义骨骼对依赖这个约定有点不舒服。如果排序很重要,我会改用:
<form>
<input name="param[0]" />
<input name="param[1]" />
<input name="param[2]" />
</form>
关于html - DOM 顺序是否确定张贴的表单参数的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3300201/