当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - jquery push array into array and maintain keys (multidimensional?) in post data

问题描述:

I'm trying to $.post with some form data as well as some other data in an array. I am able to push the array into the rest of the formdata but I lose the keys from they array and the data is just represented as a string, I guess what I'm asking for is a multidimensional array?

Here is the response string currently:

array(3) { ["ban_type"]=> string(11) "Account ban" ["ban-duration"]=> string(1) "2" ["test"]=> string(19) "adsasdasd,usertest2" }

The ['test'] is the array i'm trying to pass. Here is the data I am passing where 'getSelectedUsers()' is the array:

 var formdata = $('#ban-form').serializeArray();

formdata.push({name:'test', value:getSelectedUsers()});

Is there anyway I can get the value of ['test'] to hold the actual array with keys?

EDIT: here is the post used in a function:

$.post(action, request_data, function(data)

{

$(response_element).html(data);

});

and here is the call with the data passed:

 ajaxPost(action, formdata, "body");

if I just pass the array I get the response with keys:

ajaxPost(action, {test:getSelectedUsers()}, "body");

EDIT: for clarifcation on what I am asking - Is there any way I can append/push a normal array into the serialized formdata array such that it is multidimensional and can hold the array and it's keys rather than just the string: ["test"]=> string(19) "adsasdasd,usertest2" (I need the keys e.g [1] => 'usertest2' so that I can access them later?)

网友答案:

Apologies if the question was unclear, solved by manually creating the array rather than trying to serialize the form data and appending the array:

var data = 
   {
     form_data:
     {
         ban_type: type,
         ban_duration: duration
     },
     ban_data: getSelectedUsers()
   };
分享给朋友:
您可能感兴趣的文章:
随机阅读: