欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

ajax异步读取后台传递回的下拉选项的值方法

时间:2021-09-27 09:44:02|栏目:JavaScript代码|点击:

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;">&nbsp;*</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;
}
}

上一篇:Flexible.js可伸缩布局实现方法详解

栏    目:JavaScript代码

下一篇:Javascript如何实现双指控制图片功能

本文标题:ajax异步读取后台传递回的下拉选项的值方法

本文地址:http://www.codeinn.net/misctech/177667.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有