does DOM order determine the order of form paramet

2020-02-12 09:03发布

问题:

If I have a form like this:

<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?

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.

回答1:

This is the standard. I find that the W3C specification is very useful and readable.

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.

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>