0

I use this code for my form

var formData = $("#form").serializeArray();

now I want to push an array into formData variable like this

[{"name":"xxxx"},{"name":"xxxx"}]

or push the array to object of the formData

Update: I want to send the data via ajax to server

any solution?

  • `serializeArray()` returns an array, so normal array operations like push or concat will work, along with all the details that those methods involve. – Taplar Mar 13 '19 at 17:25
  • can you give me any example? I tried many solutions but I got nothing – keyvan ashouri Mar 13 '19 at 17:49
  • https://jsfiddle.net/5zL1cpod/ Fairly simple test case to run. Your question is primarily about how to push an array to an existing array, where there should already be duplicate questions about on the site. – Taplar Mar 13 '19 at 17:53
  • there is a problem, I send the data via ajax to server and I see nothing data have sent it says undefined – keyvan ashouri Mar 13 '19 at 18:32

1 Answers1

0

Very well I answer my question, forget about the serializeArray() so

    var formData = {};
    formData['X1'] = [];
    item1 = {}
    item1 ["name"] = "XXX";
    formData['X1'].push(item1);

the result is:

{[{"name":"XXX"},{"name":"XXX"}]}

then send formData via ajax, it's easy, isn't it?