日期:2014-05-16  浏览次数:20793 次

ajax struts2 给下拉框赋值
一、js代码
$(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');      
       }
    });
//选择渠道信息时候,加载版本信息 
$("#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>");
   }
  });  }); 
二、html页面关键代码 
<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>
三、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(){