ajax異步讀取后臺(tái)傳遞回的下拉選項(xiàng)的值方法

jsp中html相關(guān)代碼

<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 style="font-size:12px;color:red;" id="mesjieci">
   </td>

</tr>

js中相關(guān)代碼

<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>

struts-config.xml中相關(guān)配置

<!-- ajax異步加載字典 -->
<action path="/dictsaction" type="oa.sys016.oagzxxgl.action.dictsaction" scope="request" />

java后臺(tái)代碼

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;
/**
 * 通過(guò)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");


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異步讀取后臺(tái)傳遞回的下拉選項(xiàng)的值方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持碩編程。

相關(guān)文章
亚洲国产精品第一区二区,久久免费视频77,99V久久综合狠狠综合久久,国产免费久久九九免费视频