Ticket #957 (closed bug: fixed)
serialize() doesn't work for checkboxes (jQuery 1.1.1)
| Reported by: | mager@… | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.2 |
| Component: | ajax | Version: | 1.1 |
| Keywords: | serialize | Cc: | |
| Blocking: | Blocked by: |
Description (last modified by john) (diff)
Short example code with two checkboxes, the second one checked:
<html> <head>
<script language="javascript" src="jquery/jquery.js"></script> <title>JS - Test</title> <script language="Javascript"> $(function() {
$('#submit').click(function(){
console.log($("input").serialize());
});
}) $("input[@type=text]").serialize(); </script>
</head> <body> <input type="checkbox" name="chk_1"><br /> <input type="checkbox" name="chk_2" checked><br /> <button id="submit" type="button">Show me!</button> </body> </html>
Firebug console shows "chk_1=on&chk_2=on", what can't be true.
Change History
comment:2 Changed 6 years ago by malsup
This is a result of the core serialize method not comprehending "successful" controls.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Well the problem may be more with Firefox as the default value of a checkbox in firefox is "on"
Note that the value of the checkbox and the checked state are different.
This behavior does not happen in safari. In safari you would get this: chk_1=&chk_2=
I usually do something like this before serializing:
$("input[@type='checkbox']").each(function() { this.value = this.checked?'on':'off'; });