Jquery SerializeArray 和推送对象列表

Posted

技术标签:

【中文标题】Jquery SerializeArray 和推送对象列表【英文标题】:Jquery SerializeArray and Push Object list 【发布时间】:2020-02-20 15:39:10 【问题描述】:

我正在使用 SerializeArray 并尝试推送对象列表,但在服务器端始终为空

var $form = $('#personelReqForm');
var model = $form.serializeArray();

  model.Leavemembers = [],
  model.MemberLanguages = [];


            $.map($('#tblLeaveingMember tbody tr[data-id="0"]'),
                function(obj) 
                    model.LeaveMembers.push(
                            MembershipId: $(obj).attr('data-mid'),
                            LeavingDate: $(obj).attr('data-date')

                    );


                );

模型视图结束..

    (16) […, …, …, …, …, …, …, …, …, …, …, …, …, …, …, …, LeaveMembers: Array(1), MemberLanguages: Array(0)]
    0: name: "Id", value: "0"
    1: name: "TitleId", value: ""
    2: name: "CompanyCardId", value: "2"
    3: name: "DepartmentId", value: "175"
    4: name: "ReasonId", value: "94"
    5: name: "ContractTypeId", value: ""
    6: name: "PersonnelGroupId", value: ""
    7: name: "MembershipLevelId", value: ""
    8: name: "TargetedStartDate", value: "24.10.2019"
    9: name: "StationId", value: ""
    10: name: "LanguageId", value: ""
    11: name: "LanguageLevelId", value: ""
    12: name: "Keywords", value: "<p><br></p>"
    13: name: "Certificates", value: "<p><br></p>"
    14: name: "ComputerSkills", value: "<p><br></p>"
    15: name: "Description", value: "<p><br></p>"
    LeaveMembers: Array(1)
    0: MembershipId: "172024", LeavingDate: "24.10.2019"
    length: 1
    __proto__: Array(0)

我如何使用 ajax 发送所有对象?谢谢你。。

【问题讨论】:

您将所有对象(例如 MVC 应用程序或其他东西)发送到哪里? 【参考方案1】:

首先我将数组的迷你代码写成 json

  function arrayToJSON(array) 
            var _ = ;

            $.map(array,
                function(obj) 
                    _[obj.name] = obj.value;

                );
            return _;
        

 var modelJSONobj = arrayToJSON(model);

和其他两个对象List Leavemembers = [], 成员语言 = [];

将它们扩展到 JsonObject var dat = $.extend(modelJSONobj , LeaveMembers: LeaveMembers ); dat = $.extend(dat, MemberLanguages: MemberLanguages );

【讨论】:

以上是关于Jquery SerializeArray 和推送对象列表的主要内容,如果未能解决你的问题,请参考以下文章

jQuery 之 serialize() serializeArray()

Ajax jQuery serialize() & serializeArray() textarea 未以 Django 形式提交

jQuery - 基于serializeArray的serializeObject

分享一个 jquery serializeArray()序列化方法

JQuery中serialize()serializeArray()和param()的使用方法

jquery serializeArray()serialize()增加数据