- <script type="text/javascript">
- $(function(){
- linkage.init('state','country','city');
- linkage.init('state1','country1','city1');
- });
- </script>
- </head>
- <body >
- 出发地:
- <select id="state"></select>
- <select id="country"></select>
- <select id="city"></select>
- <br>
- 到达地:
- <select id="state1"></select>
- <select id="country1"></select>
- <select id="city1"></select>
- </body>
- function linkage(){}
- $(function(){
- //linkage.init("state","country","city");
- });
- linkage.init = function(levelOne, levelTwo, levelThree){
- linkage.data = [];
- levelOne = "#" + levelOne;
- levelTwo = "#" + levelTwo;
- levelThree = "#" + levelThree;
- var html = "";
- var url = "linkage.action";//访问地址
- //JQuery访问数据
- $.getJSON(url, function(data){
- linkage.data = data;
- $(levelOne).html(linkage.getOptionHtmlByLevel(1));//洲区域信息
- $(levelTwo).html(linkage.getOptionHtmlById($(levelOne).val()));//国家信息
- $(levelThree).html(linkage.getOptionHtmlById($(levelTwo).val()));//城市信息
- // 为下拉levelOne框绑定onchange事件
- $(levelOne).change(function(){
- $(levelTwo).html(linkage.getOptionHtmlById($(levelOne).val()));//国家信息
- $(levelThree).html(linkage.getOptionHtmlById($(levelTwo).val()));//城市信息
- });
- // 为下拉levelTwo框绑定onchange事件
- $(levelTwo).change(function(){
- $(levelThree).html(linkage.getOptionHtmlById($(levelTwo).val()));//城市信息
- });
- });
- };
- /**
- * 获得下拉框的选项html
- * @param data
- * @return html
- */
- linkage.getOptionHtml = function(data){
- var html = "";
- html = "<option value="+data.id+">" + data.locationName + "</option>";
- return html;
- };
- /**
- * 根据副ID查找数据
- * @param subId
- * @return html
- */
- linkage.getOptionHtmlById = function(subId){
- var html = "";
- for(i = 0 ; i < linkage.data.length ; i++){
- if(linkage.data[i].subId == subId){
- html += linkage.getOptionHtml(linkage.data[i]);
- }
- }
- return html;
- };
- /**
- * 根据区域级别查找数据
- * @param level
- * @return html
- */
- linkage.getOptionHtmlByLevel = function(level){
- var html = "";
- for(i = 0 ; i < linkage.data.length ; i++){
- if(linkage.data[i].locationLevel == level){
- html += linkage.getOptionHtml(linkage.data[i]);
- }
- }
- return html;
- };
- public void getAllLocationInfo(){
- List<LocationBean> locationList = this.linkageService.findAllLocation();
- HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
- HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
- try {
- request.setCharacterEncoding("utf-8");
- response.setCharacterEncoding("utf-8");
- JSONArray json = JSONArray.fromObject(locationList);
- response.getWriter().write(json.toString());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
注意:此用到了JSON转换包
jar包:json-lib-0.4.jar,commons-beanutils.jar
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。