Bug Tracker

Opened 13 years ago

Closed 12 years ago

#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:
Blocked by: Blocking:

Description (last modified by john)

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 (4)

comment:1 Changed 13 years ago by digid

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

comment:2 Changed 13 years ago by malsup

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

comment:3 Changed 12 years ago by arrix

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.

comment:4 Changed 12 years ago by john

Description: modified (diff)
Milestone: 1.2
need: Review
Resolution: fixed
Status: newclosed

Fixed in jQuery 1.2.

Note: See TracTickets for help on using tickets.