macek / jquery-serialize-object

Converts HTML form into JavaScript object
Other
1.11k stars 353 forks source link

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

Open nathankoop opened 9 years ago

nathankoop commented 9 years ago

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 9 years ago

Hey Nathan,

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

typical POST request

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


jQuery serialize

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


jQuery serializeObject

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.

nathankoop commented 9 years ago

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 9 years ago

@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! :wink:

nathankoop commented 9 years ago

cool, thanks.