开发者

jquery: possible to create GET string with nested arrays using serialize()?

I have a page with 3 forms on it.

To create a valid get(or post) data str with each form as a nested array nested arrays.

psuedo-code for what i want (just don't know how to wrap them in nested arrays..):

var data_str = $('#form1').serialize() + $('#form2').serialize(开发者_运维百科) + $('#form3').serialize();
$.ajax(.....

desired output on processing file:

print_r($_GET);
/*
{
    ['form1'] => .....nested array here..
    ['form2'] => .....nested array here..
    ['form3'] => .....nested array here..
}
*/


$.get({
data:{
   form1:$('#form1').serialize(),
   form2:$('#form2').serialize(),
   form3:$('#form3').serialize()
},

//other options


I'm not a javascript pro but I've built a solution and it works, enjoy:

function mutliple_forms_to_data_str(array_of_form_ids){
    var multidim_data = {};
    $.each(array_of_form_ids, function(index,id) {
        var forms_data = $('#'+id).serializeArray();            
        var htemp = {};
        $.each(forms_data, function(index,val) {
            htemp[val.name] = val.value;
        });
        multidim_data[id] = htemp;
    });    
    return multidim_data;
}


Use array push, to do that

but honestly you best use one form no need for all three of them unless is last resource. or simply.

var data_str = {
     'form1': $('#form1').serialize(),
     'form2': $('#form2').serialize(),
     'form3': $('#form3').serialize(),
    }

PHP would read them fine as a tree like array,

You might have to use serializeArray(); instead

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜