今天小编就为大家分享一篇ajax异步读取后台传递回的下拉选项的值方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
jsp中html相关代码
<tr>
<td class="tdcell_word2">请选择展会:</td>
<td class="tdcell_word3" colspan="3">
<select name="select" id="jieci" onchange="getChange()">
<option value="">选择展会</option>
</select>
<script Language="JavaScript">
loadjieci('- 选择展会 -');
</script>
<span style="color:red;"> *</span>
<span style="font-size:12px;color:red;" id="mesjieci"></span>
</td>
</tr>
js中相关代码
<script Language="JavaScript">
function loadjieci(str) {
/** 异步加载届次 */
$.getJSON("/DictsAction.do?actionType=jieci",
function (responseData, status) {
if (status != "") {
$("#jieci").empty();
$("<option/>").html("- 选择展会 -").val("- 选择展会 -").appendTo("#jieci");
$(responseData).each(function (n) {
$("<option/>").html(this.name).val(this.code).appendTo("#jieci");
});
//默认显示请选择
$("#jieci").val(str);
} else {
alert("届次加载失败!");
}
});
}
</script>
struts-config.xml中相关配置
<!-- ajax异步加载字典 -->
<action path="/DictsAction" type="oa.sys016.oagzxxgl.action.DictsAction" scope="request" />
java后台代码
package oa.sys016.oagzxxgl.action;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import cn.com.chx.bo.AppBo;
/**
* 通过ajax加载届次选择下拉列表的选择项
* @author liu
*
*/
public class DictsAction extends Action{
private AppBo appBo = null;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 设置编码
response.setContentType("application/json;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
appBo = new AppBo();
try {
// 获取ActionType
String actionType = request.getParameter("actionType");
if (actionType != null && actionType.equals("jieci")) { // 查询届次
String jieciJson = getJieciJson();
out.print(jieciJson);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}
return null;
}
private String getJieciJson() {
// 查询届次
List jieciList;
try {
jieciList = appBo.query("SELECT ID AS CODE,ZH_NAME AS NAME FROM ZH_GZ_INFOX");
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
if (jieciList.size() > 0) {
for (int i = 0; i < jieciList.size(); i++) {
Map<String, String> maps = new HashMap<String, String>();
maps.put("code",
(String) ((HashMap) jieciList.get(i)).get("CODE"));
maps.put("name",
(String) ((HashMap) jieciList.get(i)).get("NAME"));
lists.add(maps);
}
}
// [{"id":"1","name":"CIMT2015"},{"id":"2","name":"CCMT2016"}]
JSONArray json = JSONArray.fromObject(lists);
return json.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
以上这篇ajax异步读取后台传递回的下拉选项的值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:ajax异步读取后台传递回的下拉选项的值方法
基础教程推荐
猜你喜欢
- ECSHOP中实现ajax弹窗登录功能 2023-01-31
- 第7天:CSS入门 2022-11-04
- 关于 css:WebKit (iPad) CSS3: 背景过渡闪烁 2022-09-21
- 基于bootstrap的上传插件fileinput实现ajax异步上传功能(支持多文件上传预览拖拽) 2023-02-01
- ExtJS 3.x DateField menuListeners 显示/隐藏 2022-09-15
- 深入浅析Jsonp解决ajax跨域问题 2022-12-28
- Vue+WebSocket实现在线聊天 2023-10-08
- vue的 Mixins (混入) 2023-10-08
- 解决ajax的delete、put方法接收不到参数的问题方法 2023-02-23
- 分页技术原理与实现之无刷新的Ajax分页技术(三) 2023-01-20