从数据库动态添加数据到下拉框(select)中

来源:互联网 发布:人工智能的现状 编辑:程序博客网 时间:2024/05/18 17:25

在controller中:

@RequestMapping("/type")
public String getType(HttpServletRequest req, HttpServletResponse rsp)
throws IOException {
req.setCharacterEncoding("utf-8");
rsp.setCharacterEncoding("utf-8");
System.out.println("进入gettype......");
List<Book> listType = libraryService.getBookType();
PrintWriter out=rsp.getWriter();
out.print(JSON.toJSONString(listType));
return null;
}

页面上:

<select id="sel_menu3" ></select>


js函数:

function cc(){
 $.post("<%=path%>/home/type", {} , function(data){

var res = $.parseJSON(data);
$("#sel_menu3").html("<option>请选择书的类型</option>");
$.each(res, function(i, n) {
$("#sel_menu3").append(
"<option value=" + n.genre + ">" + n.genre
+ "</option>");
});

0 0
原创粉丝点击