DOM顺序确定表单参数的顺序吗? [英] does DOM order determine the order of form parameters posted?
问题描述
如果我有这样的表单:
<form>
<input name="param[]" />
<input name="param[]" />
<input name="param[]" />
</form>
可以在提交表单时以相同的顺序接收这些参数?
Can I expect to receive these parameters in the same order when the form is submitted?
我注意到,Chrome,Rails和Webrat保留了参数的顺序,但这可能是由实现细节而不是遵守标准引起的。
I've noticed that Chrome, Rails, and Webrat preserve the order of the parameters, but that may be caused by implementation details rather than adherence to a standard.
推荐答案
This is the standard. I find that the W3C specification is very useful and readable.
应用程序/ 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.
我还没有遇到没有遵循这个惯例的浏览器,所以我会说你很安全地使用它。
I have yet to encounter a browser that doesn't follow this convention, so I'd say you're pretty safe to use it.
说,我的语义骨头依靠这个惯例有点不舒服。如果订购至关重要,我会改用:
That being said, my semantic bone is a bit uncomfortable with relying on this convention. If ordering is crucial, I would instead use:
<form>
<input name="param[0]" />
<input name="param[1]" />
<input name="param[2]" />
</form>
这篇关于DOM顺序确定表单参数的顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!