One reason for using non-standard checkbox serialization that isn't addressed in the question or in of the current answers is to only deserialize (change) fields that were explicitly specified in the serialized data - e.g. when you are using jquery serialization and deserialization to/from a cookie to save and load prefererences.
Thomas Danemar implemented a modification to the standard serialize()
method to optionally take a checkboxesAsBools
option: http://tdanemar.wordpress.com/2010/08/24/jquery-serialize-method-and-checkboxes/ - this is similar to the implementation listed above by @mydoghasworms, but also integrated into the standard serialize.
I've copied it to Github in case anyone has improvements to make at any point: https://gist.github.com/1572512
Additionally, the following modification to the "jquery.deserialize" plugin will now correctly deserialize checkbox values serialized with checkboxesAsBools
, and ignore checkboxes that are not mentioned in the serialized data: https://github.com/TaoK/jquery.deserializehttps://github.com/itsadok/jquery.deserialize