添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have a form with fields and a list of another object with items. But with jQuery $("form").serializeObject() , it is not creating the right object like here:

var functionViewModel = new Object();
functionViewModel.Id = 1;
functionViewModel.Name = "F-I-001";
functionViewModel.Localised = [];
functionViewModel.Localised.push({
  LocalisedId: '1|1',
  Subject: "F-N-001"
functionViewModel.CategoryViewModel = new Object();
functionViewModel.CategoryViewModel.CategoryInGroup = [];  
functionViewModel.CategoryViewModel.CategoryInGroup.push({
  Id: '1'

Any ideas?

var recEncoded = $.param( myObj); var recDecoded = decodeURIComponent( $.param( myObj ) ); alert( recEncoded ); Yah but in this case I will still need to create the object and I would like the jquery to do it by property name, property value etc – Douglas Simão Jul 12, 2017 at 13:58

Before sending data to server you need to stringify() the object. At the server you need to parse() it.

  • Use JSON.stringify() to convert object to string. Use
  • JSON.parse() to convert JSON object from string.
  • var functionViewModel = new Object();
    functionViewModel.Id = 1;
    functionViewModel.Name = "F-I-001";
    functionViewModel.Localised = [];
    functionViewModel.Localised.push({
      LocalisedId: '1|1',
      Subject: "F-N-001"
    functionViewModel.CategoryViewModel = new Object();
    functionViewModel.CategoryViewModel.CategoryInGroup = [];  
    functionViewModel.CategoryViewModel.CategoryInGroup.push({
      Id: '1'
    var str = JSON.stringify(functionViewModel)
    var obj = JSON.parse(str);
    console.log(str);
    console.log(obj);
    $.post(
      url: url, //Url of your server 
      data: JSON.stringify(obj), //data you want to send to server 
      function(data, status) {
        alert("Success");        
    

    As pointed by Heena, you can use param()

    You need to serialize/convert the object to a string before submitting it. You can use $.param() for this.

    $('#form').val($.param(functionViewModel));

    maybe I am expressing me wrong, I want jquery to read my page go field by field and create it for me – Douglas Simão Jul 12, 2017 at 13:59 Try this function objectifyForm(formArray) {//serialize data function var returnArray = {}; for (var i = 0; i < formArray.length; i++){ returnArray[formArray[i]['name']] = formArray[i]['value']; } return returnArray; } – Hassan Imam Jul 12, 2017 at 14:06

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.