封装数据为json格式

1、把多个同名(name="country")数组封装成countrys:[{"countryId":"10086","countryName":"中国"}]形式传给后台;
2、$.post时,既传输表单数据,又传输封装好的对象。

{{countryList.regionName}}
submitHandler: function(form) {
                // console.log(form1.serializeArray());
                // // return false;
                var countryChecked = new Array();         
                var list = {};
                var countrys = new Array(); 
                // allCountry = $('#model_add_form .checkName');
                allCountry = document.getElementById('model_add_form').getElementsByClassName('checkName');
                for (k in allCountry){
                    if(allCountry[k].checked)
                         countryChecked.push(allCountry[k].value)
                }
                for (let i in countryChecked){
                    var temp = JSON.parse("{"+countryChecked[i]+"}");
                    countrys.push(temp);
                }
                var submitData = form1.serializeArray();
                submitData.push({name:'countrys',value:countrys})
                error1.hide();
                App.blockUI({
                    target: '#model_add_form',
                    overlayColor: 'none',
                    cenrerY: true
                });
                $.post('/ModelAdd',submitData,function(data){
                    App.unblockUI('#model_add_form');
                    $("#ajax-modal").modal('hide');
                    ModelManager.refresh();
                }).error(function (err) {
                    App.unblockUI('#model_add_form');
                    swal({
                        title: i18n.t('swal_title_error'),
                        text: i18n.t(err.responseText),
                        confirmButtonText: ' Ok',
                        confirmButtonClass: 'btn btn-circle btn-success',
                        buttonsStyling: false
                    }).done();
                })
            }
image.png

打印出json格式:
console.log(JSON.stringify(countrys));

你可能感兴趣的:(封装数据为json格式)