Solved

jQuery: Serialize form data including selects with multiple options

Posted on 2014-10-14
2
2,275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
REST call Failing 1 39
Diff of the day 2 41
Error Handler 8 42
Ajax success not firing alert 6 41
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

732 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