1

I have a javascript function that reads an entire form into a serialized

 $.ajax({
    type: 'POST',
    url: '/cart/update.js',
    dataType: 'json',
    data: $("#add-to-cart").serialize() + '&id=' + $("select[name=id]").val(),
    success: goToCart,
    error: goToCart
  });

However it seems to be not organizing the data. It should be pattern, line 1, line 2, line 3, line 4, line 5, line 6, however it seems to be going line 1, line 2, line 3, line 4, pattern, line 5, line 6.

Is there a way to sort the serialized data somehow?

html is in proper order, pattern is first then lines 1-6.

Any help?

Steven
  • 13,250
  • 33
  • 95
  • 147
  • 2
    May I ask why it needs to be sorted? It's trivial what order it is by the time it hits the server. A get variable is a get variable. – Brad Christie Jun 16 '11 at 03:15
  • Its in a limited environment and presented in javascript, so I don't have the ability to control the output. – Steven Jun 16 '11 at 03:16
  • @Brad: Well, this actually surprises me a bit. [HTML forms spec](http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1) mandates that those values needs to be sent in the order as the input elements appear in the HTML DOM tree. Why is jQuery doing it differently? – BalusC Jun 16 '11 at 03:44

1 Answers1

0

You will have to write your own sort method there is no direct way to do it.

Take a look at this.

Sorting JavaScript Object by property value

Community
  • 1
  • 1
ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124