Alex Rivera | Logout

jQuery form post converts boolean checkboxes to "on" and "off"?

Asked 2011-03-15T20:30:06.167
36

I have a fairly simple form with a checkbox, and I noticed that my checkbox values weren't being picked up on my server side app:

$.post('CreateForm', $('#new-form').serialize(), ...

Everything else posts correctly, but I'm seeing in Firebug that it'll serialize the checkbox value as "on" or "off" instead of "true" and "false" that I get with a normal <form method="post" action="formpage"> ... what's going on here, is this expected behavior? My server side model binder doesn't equate "on" with "true" and thus drops the value. Obviously I could change the model binder, but wanted to make sure I wasn't doing it wrong.

Edit:

Here's my markup:

<input type="checkbox" name="CheckboxValue" >

jQuery version 1.4.4

In Firebug here's the resulting post:

... other variables &CheckboxValue=on
Edit
Report

1 Answer

71

The problem is that your checkboxes do not have value attributes. When jQuery serializes the form, it uses 'on' as the default value.

You should give your checkbox a value:

<input type="checkbox" name="CheckboxValue" value="true" />

See example here: http://jsfiddle.net/U6Sbw/1/

answered 2011-03-15T20:47:04.727

Your Answer