Solved

jQuery: Serialize form data including selects with multiple options

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SharePoint 2013 Blog Template 2 37
Clear single text form value in jquery 2 30
Rhino javascript replace special characters 4 21
javascript date picker in php while loop 5 28
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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 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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now