$('#tfeestype').change(function(){
var p1=$(this).children('option:selected').val();//这就是selected的值
var aj = $.ajax( {
url:'http://localhost:8088/grassinfo-web/api/first_statistic_change!ajaxReturn.action',// 跳转到 action
<!--data为传入后台action的参数,name为传入的参数-->
data:{
name:p1
},
type:'post',
cache:false,
dataType:'json',
<!--data是后台json返回值,与上面data不同,可任意取名-->
success:function(data) {
if(data!=null){
var str = "";
str += "<option value=\"null\" >请选择</option>";
$.each(data, function(i, n) {
str += "<option value=\""+ n.name +"\" >"+ n.name +"</option>";
});
$("#tfeeCounty").html(str);
}
}
public String ajaxReturn(){
String cityName = getReq().getParameter("name");
Object[] objdate = null;
UserData ud = new UserData();
StringBuffer ajaxSql = new StringBuffer();
ajaxSql.append("select res.country,res.mm from(select city, country, count(1) mm "
+" from (select substr(t.address, "
+" instr(t.address, '_', 1, 1) + 1, "
+" instr(t.address, '_', 1, 2) - "
+" instr(t.address, '_', 1, 1) - 1) city, "
+" substr(t.address, "
+" instr(t.address, '_', 1, 2) + 1, "
+" instr(t.address, '_', 1, 3) - "
+" instr(t.address, '_', 1, 2) - 1) country "
+"from tuser t "
+"where t.address is not null "
+"and t.address like '%浙江省%' "
+"and t.user_state = 1) t "
+"group by t.city, country)res where city='"+cityName+"'");
Map<String, UserData> ajaxMap = jdbcTemplate.query(ajaxSql.toString(),
objdate, new ResultSetExtractor<Map<String, UserData>>() {
@Override
public Map<String, UserData> extractData(ResultSet rs)
throws SQLException, DataAccessException {
int mapKey=0;
Map<String, UserData> map = new LinkedHashMap<String, UserData>();
try {
while (rs.next()) {
String county = rs.getString("COUNTRY");
String sum = rs.getString("MM");
UserData udData = new UserData();
udData.setName(county);
udData.setNum(Integer.valueOf(sum));
map.put(String.valueOf(mapKey), udData);
mapKey++;
}
//System.out.println(com.alibaba.fastjson.JSON.toJSONString(map));
} catch (Exception e) {
logger.error("getStatisticStat", e);
// e.printStackTrace();
}
return map;
}
});
getRes().setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = getRes().getWriter();
out.print(com.alibaba.fastjson.JSON.toJSONString(ajaxMap));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out.flush();
out.close();
}
}
return null;
}
本文介绍了一种使用jQuery实现的动态加载下拉菜单选项的方法。当用户选择特定项时,通过Ajax请求从服务器获取相关数据,并更新另一个下拉菜单的内容。具体实现了根据所选城市动态显示对应县区的功能。
347

被折叠的 条评论
为什么被折叠?



