解决Jquery下拉框数据动态获取的问题

前端之家收集整理的这篇文章主要介绍了解决Jquery下拉框数据动态获取的问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

select.jsp

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> nofollow" > My JSP 'select.jsp' starting page
<<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="pragma" content="no-cache"&gt;
<<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="cache-control" content="no-cache"&gt;
<<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="expires" content="0"&gt; 
<<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;
<<a href="/tag/Meta/" target="_blank" class="keywords">Meta</a> http-equiv="description" content="This is my page"&gt;
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
-->
<script type="text/javascript" src="js/jquery-2.1.1.min.js" charset="utf-8"&gt;</script>
<script type="text/javascript"&gt;

    function get_app_type() {

        $.ajax({
            type: "post",url: "AppTypeShow.action",//<a href="/tag/huoqu/" target="_blank" class="keywords">获取</a>json数据
            dataType: "json",success: function(data) {
                var d = eval("(" + data + ")");
                for(var i = 0; i < d.length; i++) {

var id = d[i].id;
var name = d[i].name;
var opt = "";
$("#appType").append(opt);
}
},error: function() {
alert("系统异常,请稍后再试!")
}
});

    }

    function get_app_class() {

        $.ajax({
            type: "post",url: "AppClassShow.action",dataType: "json",success: function(data) {
                var d = eval("(" + data + ")");
                for(var i = 0; i < d.length; i++) {

var id = d[i].id;
var name = d[i].name;
var opt = "";
$("#appClass").append(opt);
}
},error: function() {
alert("系统异常,请稍后再试!")
}
});

    }

    $(document).ready(function() {

        get_app_type();
        get_app_class();

    });
</script>

struts.xml

<action name="*JsonAction" method="{1}" class="jquery.chisj.action.JsonAction"&gt; <result name="fail"&gt;error.jsp</result> <result type="json"&gt; <param name="root"&gt;result</param> </result> </action> <action name="AppTypeShow" class="jquery.chisj.action.NtAppAction" method="appTypeShow"&gt; <result name="fail"&gt;error.jsp</result> <result type="json"&gt; <param name="root"&gt;result</param> </result> </action> <action name="AppClassShow" class="jquery.chisj.action.NtAppAction" method="appClassShow"&gt; <result name="fail"&gt;error.jsp</result> <result type="json"&gt; <param name="root"&gt;result</param> </result> </action> </package>

NtAppAction.java

public String appTypeShow() { System.out.println("---app type show---"); List<APPType> appTypeList = new ArrayList<APPType>(); try { APPType appType_1 = new APPType(); APPType appType_2 = new APPType(); appType_1.setId(Short.valueOf("1")); appType_1.setName("Android"); appType_2.setId(Short.valueOf("2")); appType_2.setName("iOS"); appTypeList.add(appType_1); appTypeList.add(appType_2); JSONArray jsonArray = JSONArray.fromObject(appTypeList); result = String.valueOf(jsonArray); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public String appClassShow() { System.out.println("---app class show---"); List<APPClass> appClassList = new ArrayList<APPClass>(); try { APPClass appClass_1 = new APPClass(); APPClass appClass_2 = new APPClass(); appClass_1.setId(Short.valueOf("1")); appClass_1.setName("种类1"); appClass_2.setId(Short.valueOf("2")); appClass_2.setName("种类2"); appClassList.add(appClass_1); appClassList.add(appClass_2); JSONArray jsonArray = JSONArray.fromObject(appClassList); result = String.valueOf(jsonArray); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; } public String getResult() { return result; } public void setResult(String result) { this.result = result; }

}

以上这篇解决Jquery下拉框数据动态获取的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。

猜你在找的jQuery相关文章