macek/jquery-serialize-object

Does not serialize unchecked checkboxes.

thedaniel opened this issue · 5 comments

if you have a number of checkboxes, only the ones that are checked will be serialized (with "on")

(I understand that this is a result of using $.serializeArray under the hood, but it's still unfortunate)

The same behavior exists if you were to submit the form for server-side processing; unchecked inputs are not submitted with the rest of the form data.

I would blame the HTML behavior of the <input type="checkbox"> before $.serializeArray or my plugin.

What's the use case here? What value would you expect unchecked boxes to have? null? an empty string? 0? false?

If you can come up with an elegant solution here, or even ideas of how you might want to implement that, I'd be curious to see the result.

Follow up question: Couldn't you just check the returned json for the input you're looking for? If the property doesn't exist, it means the input was not checked.

@thedaniel, this seems to work:

<input type="hidden" name="checkboxName" value="false"/>
<input type="checkbox" name="checkboxName" value="true"/>

(if the checkbox is checked, it will overwrite the value of the hidden field)

Sorry to ignore this for so long. @macek I think you make a good point that it's sensible not to serialize unchecked inputs because they are not sent in a submitted form (which is probably why serializeArray does it that way too). gonna close this if that's all right, it's not an issue for me anymore.