tr>
td class="tdcell_word2">請(qǐng)選擇展會(huì):/td>
td class="tdcell_word3" colspan="3">
select name="select" id="jieci" onchange="getChange()">
option value="">選擇展會(huì)/option>
/select>
script Language="JavaScript">
loadjieci('- 選擇展會(huì) -');
/script>
span style="color:red;">nbsp;*/span>
span style="font-size:12px;color:red;" id="mesjieci">/span>
/td>
/tr>
script Language="JavaScript">
function loadjieci(str) {
/** 異步加載屆次 */
$.getJSON("/DictsAction.do?actionType=jieci",
function (responseData, status) {
if (status != "") {
$("#jieci").empty();
$("option/>").html("- 選擇展會(huì) -").val("- 選擇展會(huì) -").appendTo("#jieci");
$(responseData).each(function (n) {
$("option/>").html(this.name).val(this.code).appendTo("#jieci");
});
//默認(rèn)顯示請(qǐng)選擇
$("#jieci").val(str);
} else {
alert("屆次加載失?。?);
}
});
}
/script>
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加載屆次選擇下拉列表的選擇項(xiàng)
* @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 {
// 設(shè)置編碼
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");
ListMapString, String>> lists = new ArrayListMapString, String>>();
if (jieciList.size() > 0) {
for (int i = 0; i jieciList.size(); i++) {
MapString, String> maps = new HashMapString, 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異步讀取后臺(tái)傳遞回的下拉選項(xiàng)的值方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。