Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing an array in jQuery

How to prepare an array of element for $.ajax submit?

Here images return ["val1","val2"], but after I use $.param(images) I get the following:

undefined=undefined&undefined=undefined


Here is my code:

$('#rem_images').click(function() {
    var images = new Array();
    $('.images_set_image input:checked').each(function(i) {
        images[i] = $(this).val();
    });
    alert($.param(images));

    return false;
}


Generally the idea is to check the images to delete on the page, then on a button click loop trough all images checked and serialize an array for submit over AJAX to the PHP script.

like image 780
arma Avatar asked Nov 21 '10 18:11

arma


People also ask

What is use of serializeArray in jQuery?

The serializeArray() is an inbuilt method in jQuery which is used to create a JavaScript array of objects that is ready to be encoded as a JSON string. It operates on a jQuery collection of forms and/or form controls. The controls can be of several types.

What is the difference between serialize and serializeArray?

serializeArray creates an array (not a "json array" -- there is no such thing); you can test this yourself with console. log($("#myform"). serializeArray()) . On the other hand, serialize creates a query string that's meant to be part of an HTTP request.

How can I serialize my form data?

To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .


2 Answers

You're not passing an array in proper format to $.param. From the jQuery.param docs:

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray().

The array should be an array of objects consisting of name/value pairs. You see undefined=undefined&undefined=undefined because "val1".name, "val1".value, "val2".name, and "val2".value, are all undefined. It should look something like this:

[{name: 'name1', value: 'val1'}, {name: 'name2', value: 'val2'}]

So you can construct the array like this (assuming your checkboxes have a name attribute):

$('#rem_images').click(function(){
    var images = [];
    $('.images_set_image input:checked').each(function(){
        var $this = $(this);
        images.push({name: $this.attr('name'), value: $this.val()});
    });
    alert($.param(images));
    return false;
});

Even slicker, though, is to use .map() (because functional programming is good stuff):

$('#rem_images').click(function(){
    var images = $('.images_set_image input:checked').map(function(){
        var $this = $(this);
        return {name: $this.attr('name'), value: $this.val()};
    }).get();
    alert($.param(images));
    return false;
});
like image 110
Matt Ball Avatar answered Oct 03 '22 17:10

Matt Ball


See the docs for $.param:

If the object passed is in an Array, it must be an array of objects in the format returned by .serializeArray()

[{name:"first",value:"Rick"},
{name:"last",value:"Astley"},
{name:"job",value:"Rock Star"}]

That means you need to generate your array in the same way:

$('.images_set_image input:checked').each(function(i){
    images.push({ name: i, value: $(this).val() });
});
like image 40
casablanca Avatar answered Oct 03 '22 18:10

casablanca