ajax接收后端传回的jsonarray数据,追加到<select>
import net.sf.json.JSONArray;
@RequestMapping(value = "admin/selectLearnPackge/{trainingId}", method = RequestMethod.POST) public void selectLearnPackge(HttpServletResponse response,@PathVariable int trainingId) throws IOException { List<LearnPackage> learnPackageList = learnPackageService.getLearnPackageList(trainingId); JSONArray jsonArray = JSONArray.fromObject(learnPackageList); response.setContentType("text/html;charset=utf-8"); response.getWriter().println(jsonArray); }
function selectLearnPackge() { var trainingId = $("#trainingId").val(); $.ajax({ url : "admin/selectLearnPackge/"+trainingId, async : true, type : "POST", dataType : "json", // 成功后开启模态框 success:function(result) { $("#learnPackageId").empty(); $("#learnPackageId").append("<option value=''>请选择</option>"); $.each(result, function (i, item) { $("#learnPackageId").append("<option value="+item.id+">"+item.packageName+"</option>"); }); }, error : function() { alert("请求失败"); } }); }
<select id="trainingId" style="margin-left: 20px;width: 150px;" name="trainingId" onchange="selectLearnPackge()"> <option value="">请选择...</option> <c:forEach items="${trainingList}" var="li"> <option value="${li.trainingId}">${li.trainingName}</option> </c:forEach> </select> <select id="learnPackageId" style="margin-left: 20px;width: 150px;" name="learnPackageId"> <option value="">请选择...</option> </select>