jquery动态实现填充下拉框

项目需要实现一个功能 当点下拉框时动态加载后台数据。
这是后台数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
Map<String, String> product_names = DataManager.getGroup_names();
StringBuffer sb = new StringBuffer();
sb.append("{");
Set<String> keys = product_names.keySet();
for(Iterator<String> iter = keys.iterator();iter.hasNext();) {
String key = iter.next();
sb.append(product_names.get(key)+":");
sb.append("['"+key+"'],");
}
sb.deleteCharAt(sb.length()-1);
sb.append("}");
response.getWriter().write(sb.toString());
}

前台设值
$.ajax({
type:"post",
url:"InitData",
data:"",
success:function(data) {
var map =  eval("("+data+")");
$.each(map,function(key,values){
$("<option value="+values+">"+key+"</option>").appendTo("#p_name");
}
); 
}
}); 

你可能感兴趣的:(java,jquery,Ajax)