ajax异步读取后台传递回的下拉选项的值方法
程序员文章站
2022-03-04 17:07:51
jsp中html相关代码
请选择展会:
&l...
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异步读取后台传递回的下拉选项的值方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
上一篇: PowerPoint2003/2007幻灯片常用快捷键大全
下一篇: 一个男生的牛B日记