封裝數(shù)據(jù)為json格式

1、把多個(gè)同名(name="country")數(shù)組封裝成countrys:[{"countryId":"10086","countryName":"中國"}]形式傳給后臺(tái);
2、$.post時(shí),既傳輸表單數(shù)據(jù),又傳輸封裝好的對(duì)象。

<div  class="col-md-6">
                                <input type="checkbox" class="checkName"  value='"countryId":"{{countryList.id}}","countryName":"{{countryList.regionName}}"' {{region_selected}}>{{countryList.regionName}}</input>
                            </div>
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: '<i class="fa fa-check"></i> Ok',
                        confirmButtonClass: 'btn btn-circle btn-success',
                        buttonsStyling: false
                    }).done();
                })
            }
image.png

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容