ajax struts2 给下拉框赋值
一、js代码
[java]
$(function(){
$.ajax({
type:'post',
url:'${ctx}/dictionary/listChannel.do',
data:'',
dataType:'json',
success:function(json){
for(var i=0; i< json.length;i++){
$("#channel_id").append("<option value='"
+json[i].value_Id+"'>"
+json[i].value+"</option>");
}
},
error:function(){
alert('error');
}
});
//选择渠道信息时候,加载版本信息
[java]
$("#channel_id").change(function(){
var channel_id=$("#channel_id").val();
$("#version_id").empty();
if (""!= channel_id) {
//查询版本信息
$.ajax({
type:'post',
url:'${ctx}/dictionary/listVersion2.do',
data:'Id='+channel_id,
dataType:'json',
success:function(json){
if(null != json){
$("#version_id").append("<option value=''>---请选择---</option>");
for(var i=0; i< json.length;i++){
$("#version_id").append("<option value='"
+json[i].value_Id+"'>"
+json[i].value+"</option>");
}
}
},
error:function(){
alert('该渠道下没有版本信息');
}
});
}else{
$("#version_id").append("<option value=''>---请选择---</option>");
}
}); });
[java]
二、html页面关键代码
[java]
<td align="left" height="18" bgcolor="#ecf6fa">
<span class="STYLE8">渠道标识:</span>
<select id="channel_id" name="packageBean.CHANNEL_ID" class="selectstyle200">
<option value="">---请选择---</option>
</select>
</td> <td align="left" height="18" bgcolor="#ecf6fa">
<span class="STYLE8">版本标识:</span>
<select id="version_id" name="packageBean.VERSION_ID" class="selectstyle200">
<option value="">---请选择---</option>
</select>
</td>
[java]
三、struts2 action中的方法package com.ecp.web.dictionary.action;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.alibaba.fastjson.JSON;
import com.ecp.web.dictionary.business.DictionaryManager;
import com.hzdracomsoft.base.BaseAction;
import com.hzdracomsoft.common.LogUtil;
import com.hzdracomsoft.javabean.Dictionary;
/***
* 获取字典表信息
* @author ZhuangZi
* @version $Id: DictionaryAction.java,v 0.1 2013-1-29 上午10:55:53 ZhuangZi Exp $
*/
public class DictionaryAction extends BaseAction{
private static LogUtil log = LogUtil.getInstance(DictionaryAction.class);
private DictionaryManager dictionaryManager;
private Dictionary dictionary;
private List<Dictionary> listDictionary;
private String Id;
/***
* 获取渠道信息
*
*/
public void listChannel(){
String json="";
try{
listDictionary = dictionaryManager.handleListChannel();
json=JSON.toJSONString(listDictionary);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println(json);
out.flush();
out.close();
}catch(Exception e){
log.error(e);
}
}
/***
* 获取版本信息
*
*/
public void listVersion2(){
String json="";
try{
listDictionary=dictionaryManager.handleListVersion(Id);
json=JSON.toJSONString(listDictionary);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println(json);
out.flush();
out.close();
}catch(Exception e){
log.error(e);
}
}
/***
* 获取下载包表信息
* @return
*/
public void listPackage(){
String json="";
try{
listDictionary=dictionaryManager.handleListPackage(Id);
json=JSON.toJSONString(listDictionary);
HttpServletResponse response=ServletActionContext.getResponse();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out;
out = response.getWriter();
out.println(json);
out.flush();
out.close();
}catch(Exception e){
log.error(e);
}
}
public Dictionary getDictionary() {
return dictionary;
}
public void setDictionary(Dictionary dictionary) {
this.dictionary = dictionary;
}
public List<Dictionary> getListDictionary() {
return listDictionary;
}
public void setListDictionary(List<Dictionary> listDictionary) {
this.listDictionary = listDictionary;
}
public DictionaryManager getDictionaryManager() {
return dictionaryManager;
}
public void setDictionaryManager(DictionaryManager dictionaryManager) {
this.dictionaryManager = dictionaryManager;
}
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
}