Skip to main content

Bug Tracker

Side navigation

#957 closed bug (fixed)

Opened February 14, 2007 03:22PM UTC

Closed September 15, 2007 02:43AM UTC

serialize() doesn't work for checkboxes (jQuery 1.1.1)

Reported by: mager@falter.at Owned by:
Priority: major Milestone: 1.2
Component: ajax Version: 1.1
Keywords: serialize Cc:
Blocked by: Blocking:
Description

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.

Attachments (0)
Change History (4)

Changed April 01, 2007 08:32AM UTC by digid comment:1

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';
     });

Changed May 02, 2007 12:51AM UTC by malsup comment:2

This is a result of the core serialize method not comprehending "successful" controls.

Changed August 26, 2007 08:04AM UTC by arrix comment:3

According to the roadmap, jQuery 1.2 will use new form/field serialization methods imported from Form plugin. Most related bugs will be fixed by the time.

Changed September 15, 2007 02:43AM UTC by john comment:4

description: 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.\ 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. \
milestone: → 1.2
need: → Review
resolution: → fixed
status: newclosed

Fixed in jQuery 1.2.