VAAA VAAA - 3 months ago 12
jQuery Question

Jquery how to append and serialize an array

I have a form that it's being serialized:

// save data
var data = $('#siteForm input').serialize();


Data will contain the following:

Site=asdf&StrDateUpdate=08%2F31%2F2016&StartingBR=1&EndingBR=1&HandsPlayed=1&ScreenShot=www.screenshoturl2.com

Now I have an array of strings:

var contents = $('#screenlist').html().split(/\n|\<br>/).filter(function(v) {
return /\w+/.test(v);
});
var links = {};

for (var k in contents) {
links[k] = contents[k];
}


And now I need to append
links
array into my data serialized content so my server can catch it.


Any clue?

Answer

I think you're looking for $.param().

Documentation: http://api.jquery.com/jquery.param/

Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request...

var obj   = {foo:'b a r',blah:'ha=lb'},
    query = $.param(obj);

$('#output').text(query);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="output"></div>