Solved

jQuery: Serialize form data including selects with multiple options

Posted on 2014-10-14
2
2,174 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)

679 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