关于 FormData 对象数组传值总结

关于 FormData 对象数组传值总结

jQuery Java Spring MVC

jQuery 代码

提示: Ncr 已经存在于实体类中。

   // 提交数据
       $('#submit-btn').click(function(){
           // 数据获取
           var formData = {};
           var inputNum = $('.form-horizontal label').length;
           // 判断控件类型,以 formData 对象的形式向后台传数据
           for(var i=0;i<inputNum;i++){
               formData['ncr['+i+'].field'] = $('.form-horizontal label').data('code');
               if(type = "input"){               
                   formData['ncr['+i+'].inner'] = $('.form-horizontal .input-delete').eq(i).children().val();
               }else if(type = "select"){
                   var myStr = $('.form-horizontal .input-delete').eq(i).children().val().join("-");
                   formData['ncr['+i+'].inner'] = myStr; 
               }else if(type = "sort"){
                   var myStr2 = $('.form-horizontal .input-delete').eq(i).children().val().split(",").join("-");
                   formData['ncr['+i+'].inner'] = $('.form-horizontal input').val(); 
               }
           }
       });

在实体类中创建 Ncr List集合

private List<Ncr> ncr;

public List<Ncr> getNcr() {
    return ncr;
}
public void setNcr(List<Ncr> ncr) {
    this.ncr = ncr;

在Ncr.java List 集合中添加实例

public class Ncr {
    private String field;
    private String inner;
    public String getField() {
        return field;
    }
    public void setField(String field) {
        this.field = field;
    }
    public String getInner() {
        return inner;
    }
    public void setInner(String inner) {
        this.inner = inner;
    }    
}

在 controller 中调用

@RequestMapping(value = "recive") 
@ResponseBody  
 public Map<String, Object> recive(Ncr entity) {
 }

数据类型

{ncr[0].field:"name",ncr[0].inner:"fz",ncr[1].field:"age",ncr[1].inner:"24"}
    原文作者:spoiler
    原文地址: https://segmentfault.com/a/1190000014777176
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞