Spring使用Ajax动态生成复选框

时间:2018-12-28 08:02:28

标签: ajax spring hibernate spring-boot

在我的Spring项目中,我必须使用Ajax动态生成复选框。我能够从数据库生成复选框,但不保留其选中的值。

下面是对控制器的Ajax调用:

$.ajax({
        type: "GET",                        
        url: "http://localhost:8080/ssdms/getLoc",
        data: {dept_id: dept_id},
        dataType: 'json',
        success: 
    function(response){                                   
            for ( var i = 0, len = response.length; i < len; ++i) {
                var location = response[i];
                $('#deptLocation').append("<input type=\"checkbox\" value=\"" + location.id + "\">"+location.desc + "</input><br/>");
        }               
    }
  }); 

下面是控制器代码:-

@RequestMapping(value = "/getLoc", method = RequestMethod.GET)
    public @ResponseBody List<Location> getLocation(@RequestParam("dept_id") int did, HttpServletRequest request,
            HttpServletResponse response) {
        Department dept = deptService.getDeptDetailsById(did);      
        List<Location> locations = deptService.getAllLocations();       
        return locations;
    }

在视图中,我正在使用以下代码:

<div class="form-group col-xs-12 col-sm-12 col-md-6">
        <label for="deptLocation">Location of Dept</label>
        <div class="checkbox" id="deptLocation">                                        
        </div>                                                              
    </div>

已保存的位置详细信息由deptService.getDeptDetailsById(did)返回,但是我无法将其映射到动态生成的复选框,因此未选中这些框。另外,当我保存表单时,null被保存在部门表的“位置”列中。我无法在动态生成的复选框代码中使用th:field="*{deptLocation}",因此它未与实体链接。

0 个答案:

没有答案
相关问题