從數據庫動態添加數據到下拉框(select)中

在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>");
});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章