您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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 > *</span> <span 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怎么異步讀取后臺傳遞回下拉選項的值就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。