Solved

jQuery: Serialize form data including selects with multiple options

Posted on 2014-10-14
2
2,043 Views
Last Modified: 2014-10-21
The problem is that serialize() does not properly serialize multiple options but instead it repeats the names with the different values.  I need a jQuery function that works like serialize but that manages multiple select options in a better way.

http://jsfiddle.net/bvgsqarw/
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
  $('#order').submit(function() {
   alert( $(this).serialize() );
   return false;
  });
 });
</script>
</head>
<body>
 
<form id="order" method="post" action="./">
 <input name="input1" type="text" value="Hello World" />
 <input name="input2" type="text" value="World" />
 <select name="product" size="5" multiple="multiple">
  <option value="1" selected="selected">1</option>
  <option value="2">2</option>
  <option value="3" selected="selected">3</option>
  <option value="4" selected="selected">4</option>
  <option value="5" selected="selected">5</option>
 </select>
 <input type="submit" value="submit" />
</form>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
2 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 40380630
how do you want it to handle multiple options?

any other way would require you writing a custom function
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40380644
test page : http://jsfiddle.net/yt88wnwe/
$(document).ready(function() {
    $('#order').submit(function() {
        var ser = $("[name]").not("select").serialize();
        $("select", this).each(function(i, select) {
            var s = "";
            $("option:selected", select).each(function() {
                s += (s.length>0?",":"") + $(this).val();
            });
            ser += (ser.length>0?"&":"") + $(select).attr("name") + "=" + s; 
        });
        alert(ser);
        return false;
    });
});

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question