java省市级联下拉菜单实例代码
程序员文章站
2024-03-11 17:54:19
本文实例为大家分享了java省市级联的具体代码,供大家参考,具体内容如下
1.loadareaservlet.java
package com.scce.s...
本文实例为大家分享了java省市级联的具体代码,供大家参考,具体内容如下
1.loadareaservlet.java
package com.scce.servlet; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import net.sf.json.jsonarray; import com.scce.pojo.city; import com.scce.pojo.prov; public class loadareaservlet extends httpservlet { private static final long serialversionuid = 1l; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("text/html;charset=utf-8"); request.setcharacterencoding("utf-8"); printwriter out = response.getwriter(); arraylist<prov> provs = new arraylist<prov>(); string provinceid = request.getparameter("provinceid"); if (provinceid=="" || provinceid==null) { system.out.println("加载地级市信息"); prov pro1 = new prov(); pro1.setid(0); pro1.setprovincename("湖北"); prov pro2 = new prov(); pro2.setid(1); pro2.setprovincename("湖南"); prov pro3 = new prov(); pro3.setid(2); pro3.setprovincename("广东"); prov pro4 = new prov(); pro4.setid(3); pro4.setprovincename("广西"); provs.add(pro1); provs.add(pro2); provs.add(pro3); provs.add(pro4); system.out.println(jsonarray.fromobject(provs).tostring()); //[{"id":0,"provincename":"湖北"},{}] out.print(jsonarray.fromobject(provs).tostring()); }else{ arraylist<city> citys = new arraylist<city>(); if (provinceid.equals("0")) { city city1 = new city(); city1.setid(0); city1.setcityname("武汉市"); city city2 = new city(); city2.setid(1); city2.setcityname("黄石市"); city city3 = new city(); city3.setid(2); city3.setcityname("十堰市"); citys.add(city1); citys.add(city2); citys.add(city3); } if (provinceid.equals("1")) { city city1 = new city(); city1.setid(0); city1.setcityname("长沙市"); city city2 = new city(); city2.setid(1); city2.setcityname("岳阳市"); city city3 = new city(); city3.setid(2); city3.setcityname("湘潭市"); citys.add(city1); citys.add(city2); citys.add(city3); } system.out.println(jsonarray.fromobject(citys).tostring()); out.print(jsonarray.fromobject(citys).tostring()); } } }
2.test1.html
<!doctype html> <html> <head> <title>chapter4-test1</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <link rel="stylesheet" href="../ui/themes/icon.css" type="text/css"></link> <link rel="stylesheet" href="../ui/themes/default/easyui.css" type="text/css"></link> <!-- <script type="text/javascript" src="../js/jquery-1.8.2.min.js"></script> --> <script type="text/javascript" src="../ui/jquery.min.js"></script> <script type="text/javascript" src="../ui/jquery.easyui.min.js"></script> <script type="text/javascript" src="../ui/locale/easyui-lang-zh_cn.js"></script> <script type="text/javascript"> //loadareaservlet /* var datacombobox=[{"id":1,"provincename":"湖北省"}, {"id":2,"provincename":"湖南省","selected":true}, {"id":3,"provincename":"河北省"}, {"id":4,"provincename":"河南省"} ]; */ $(function () { $("#province").combobox({ width: 150, valuefield: 'id', //1 textfield: 'provincename', // 湖北省 editable: false, // data:datacombobox //指定下拉列表的数据源 url:'../loadareaservlet', method:'post', onloadsuccess:function(data){ var provid = $("#province").combobox("getvalue"); loadcity(provid); }, onselect:function(recode){ loadcity(recode.id); } }); }); function loadcity(provid){ $("#city").combobox({ width: 150, valuefield: 'id', //1 textfield: 'cityname', // 湖北省 editable: false, // data:datacombobox //指定下拉列表的数据源 url:'../loadareaservlet?provinceid='+provid, method:'post' }); } </script> </head> <body> 省: <select id="province" > <option value="0">数据加载中..</option> </select> <br/> 市: <select id="city" > <option value="">数据加载中..</option> </select> </body> </html>
以上就是本文的全部内容,希望对大家学习java程序设计有所帮助。