macek/jquery-serialize-object

Ignore hidden field if an input with the same name already exists

nathankoop opened this issue · 4 comments

I use Asp.net MVC, when I use the @Html.EditorFor (or CheckboxFor) the Razor view engine generates two inputs, a checkbox and a hidden field. Both of these have the same id & name. (http://stackoverflow.com/questions/2860940/why-does-the-checkboxfor-render-an-additional-input-tag-and-how-can-i-get-the-v)

When I submit the form through the typical <form type='post' action='myurl'> ... <input type='submit'></form> then the MVC controller gets all the correct information, however when I use jquery ajax and this plugin (which I love btw), then the value for my field is overwritten by the hidden field's value.

I have put together a jsfiddle to repo this issue.
https://jsfiddle.net/8df7fc9k/1/

Thanks,

Nathan

macek commented

Hey Nathan,

Thanks for the detailed information and the fiddle. It allows me to help you a lot better.

typical POST request

  • checkbox checked test=true&test=false
  • checkbox unchecked test=false

Most body parsers will interpret these as {test: ["true", "false]} and {test: "false"} respectively


jQuery serialize

  • checkbox checked test=true&test=false
  • checkbox unchecked test=false

Most body parsers will interpret these as {test: ["true", "false]} and {test: "false"} respectively


jQuery serializeObject

  • checkbox checked {test: "false"} (first value is overridden by previous value)
  • checkbox unchecked {test: "false"}

This plugin was originally designed to operate this way, but in hindsight it could be considered a flaw. The 3.x branch will implement the W3C HTML JSON form submission spec which operates more in accordance with your expectations.


In 3.x you can expect

<input name="test" value="true">
<input name="test" value="false">

To serialize as

{test: ["true": "false"]}

Until then, I'm afraid this plugin may not be very useful for your particular MVC framework.

Thanks for the quick reply and the great plugin.

I have hacked my code right now to make the checkbox update the hidden field so I am still able to use your serializer.

Is there an ETA on 3.0?

macek commented

@nathankoop

Sadly I've been really busy and haven't had a lot of time to direct at the 3.x code. If I had to make an estimate, I'd say 2-3 months, but don't hold me to it! 😉

cool, thanks.