Jquery下拉框数据动态获取

废话不多说,直接上源码:

[html]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'select.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!-- 
  20.     <link rel="stylesheet" type="text/css" href="styles.css"> 
  21.     -->  
  22.     <script type="text/javascript" src="js/jquery-2.1.1.min.js" charset="utf-8"></script>  
  23.     <script type="text/javascript">  
  24.       
  25.         function get_app_type() {  
  26.           
  27.             $.ajax({  
  28.                 type: "post",  
  29.                 url: "AppTypeShow.action", //获取json数据  
  30.                 dataType: "json",  
  31.                 success: function(data) {  
  32.                     var d = eval("(" + data + ")");  
  33.                     for(var i = 0; i < d.length; i++) {  
  34.                         var id = d[i].id;  
  35.                         var name = d[i].name;  
  36.                         var opt = "<option value='" + id + "'>" + name + "</option>";  
  37.                         $("#appType").append(opt);  
  38.                     }  
  39.                 },  
  40.                 error: function() {  
  41.                     alert("系统异常,请稍后再试!")  
  42.                 }  
  43.             });  
  44.               
  45.         }  
  46.           
  47.         function get_app_class() {  
  48.           
  49.             $.ajax({  
  50.                 type: "post",  
  51.                 url: "AppClassShow.action",  
  52.                 dataType: "json",  
  53.                 success: function(data) {  
  54.                     var d = eval("(" + data + ")");  
  55.                     for(var i = 0; i < d.length; i++) {  
  56.                         var id = d[i].id;  
  57.                         var name = d[i].name;  
  58.                         var opt = "<option value='" + id + "'>" + name + "</option>";  
  59.                         $("#appClass").append(opt);  
  60.                     }  
  61.                 },  
  62.                 error: function() {  
  63.                     alert("系统异常,请稍后再试!")  
  64.                 }  
  65.             });  
  66.               
  67.         }  
  68.       
  69.         $(document).ready(function() {  
  70.           
  71.             get_app_type();  
  72.             get_app_class();  
  73.               
  74.         });  
  75.     </script>  
  76.   
  77.   </head>  
  78.     
  79.   <body>  
  80.     <table>  
  81.         <tr>  
  82.             <td align="right">APP类型:</td>  
  83.             <td align="left">  
  84.                 <select name="appType" id="appType"   
  85.                         style="margin-left: 16px; height: 30px; width: 110px; text-align: left; size: 3; color: #505050;">  
  86.                     <option value="-1">---请选择---</option>  
  87.                 </select>  
  88.             </td>  
  89.         </tr>  
  90.         <tr height="25px"><td> </td></tr>  
  91.         <tr>  
  92.             <td align="right">APP种类:</td>  
  93.                 <td align="left">  
  94.                     <select name="appClass" id="appClass"   
  95.                             style="margin-left: 16px; height: 30px; width: 110px; text-align: left; size: 3; color: #505050;">  
  96.                         <option value="-1">---请选择---</option>  
  97.                     </select>  
  98.             </td>  
  99.         </tr>  
  100.     </table>  
  101.   </body>  
  102. </html>  
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">  
  3. <struts>  
  4.   
  5.     <constant name="struts.i18n.encoding" value="UTF-8"></constant>  
  6.   
  7.     <package name="simpleton" extends="struts-default,json-default">  
  8.       
  9.         <action name="*JsonAction" method="{1}"  
  10.                 class="jquery.chisj.action.JsonAction">  
  11.             <result name="fail">error.jsp</result>  
  12.             <result type="json">  
  13.                 <param name="root">result</param>  
  14.             </result>  
  15.           
  16.         </action>  
  17.           
  18.         <action name="AppTypeShow"  
  19.                 class="jquery.chisj.action.NtAppAction"  
  20.                 method="appTypeShow">  
  21.             <result name="fail">error.jsp</result>  
  22.             <result type="json">  
  23.                 <param name="root">result</param>  
  24.             </result>  
  25.         </action>  
  26.           
  27.         <action name="AppClassShow"  
  28.                 class="jquery.chisj.action.NtAppAction"  
  29.                 method="appClassShow">  
  30.             <result name="fail">error.jsp</result>  
  31.             <result type="json">  
  32.                 <param name="root">result</param>  
  33.             </result>       
  34.         </action>  
  35.           
  36.     </package>  
  37.   
  38. </struts>      
[java]  view plain  copy
  1. /** 
  2.  *  
  3.  */  
  4. package jquery.chisj.action;  
  5.   
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import jquery.chisj.entity.APPClass;  
  10. import jquery.chisj.entity.APPType;  
  11.   
  12. import com.opensymphony.xwork2.ActionSupport;  
  13.   
  14. import net.sf.json.JSONArray;  
  15.   
  16. /** 
  17.  * @ClassName: NtAppAction 
  18.  * @Description: TODO 
  19.  * @Author: chisj chisj@foxmail.com 
  20.  * @Date 2016年1月20日 下午4:53:50 
  21.  * 
  22.  */  
  23. public class NtAppAction extends ActionSupport {  
  24.   
  25.     private String result;  
  26.       
  27.     public String appTypeShow() {  
  28.         System.out.println("---app type show---");  
  29.         List<APPType> appTypeList = new ArrayList<APPType>();  
  30.         try {  
  31.             APPType appType_1 = new APPType();  
  32.             APPType appType_2 = new APPType();  
  33.             appType_1.setId(Short.valueOf("1"));  
  34.             appType_1.setName("Android");  
  35.             appType_2.setId(Short.valueOf("2"));  
  36.             appType_2.setName("iOS");  
  37.             appTypeList.add(appType_1);  
  38.             appTypeList.add(appType_2);  
  39.             JSONArray jsonArray = JSONArray.fromObject(appTypeList);  
  40.             result = String.valueOf(jsonArray);  
  41.         } catch (Exception e) {  
  42.             e.printStackTrace();  
  43.         }  
  44.           
  45.         return SUCCESS;  
  46.     }  
  47.       
  48.     public String appClassShow() {  
  49.         System.out.println("---app class show---");  
  50.         List<APPClass> appClassList = new ArrayList<APPClass>();  
  51.         try {  
  52.             APPClass appClass_1 = new APPClass();  
  53.             APPClass appClass_2 = new APPClass();  
  54.             appClass_1.setId(Short.valueOf("1"));  
  55.             appClass_1.setName("种类1");  
  56.             appClass_2.setId(Short.valueOf("2"));  
  57.             appClass_2.setName("种类2");  
  58.             appClassList.add(appClass_1);  
  59.             appClassList.add(appClass_2);  
  60.             JSONArray jsonArray = JSONArray.fromObject(appClassList);  
  61.             result = String.valueOf(jsonArray);  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.           
  66.         return SUCCESS;  
  67.     }  
  68.       
  69.     public String getResult() {  
  70.         return result;  
  71.     }  
  72.       
  73.     public void setResult(String result) {  
  74.         this.result = result;  
  75.     }  
  76.       
  77. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值