Solved

jQuery: Serialize form data including selects with multiple options

Posted on 2014-10-14
2
1,855 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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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)

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now