通过jquery实现动态二级联动(二级数据从数据库中获取)


1、首先来看一下html页面代码

 1 <form class="form-inline search-form" id="myform" action="CustomerServlet?method=getAllCustomer" method="post">
 2                             <div class="btn-group">
 3                                 <select id="field" class="form-control" name="field">
 4                                     <option value="0">--请选择筛选条件--</option>
 5                                     <option value="customer_name">客户名称</option>
 6                                     <option value="customer_industry">所属行业</option>
 7                                     <option value="customer_source">客户来源</option>
 8                                     <option value="create_time">创建时间</option>
 9                                 </select>
10 
11                                 <!-- 显示要输入的搜索条件 -->
12                                 <div class="form-group" id="conditionContent">
13                                     
14                                 </div>
15                             </div>
16                             <div class="btn-group">
17                                 <button type="button" class="btn btn-success" id="sobtn">搜索</button>
18                             </div>
19                         </form>

2、使用jquery的change事件进行动态联动,这里只展示当option为客户来源以及所属行业时的动态联动,代码如下:

$("#field").change(function(){
        $("#conditionContent").empty();
        //获取条件
        var condition=$(this).find("option:checked").val();
        
        if(condition=="customer_name"){
            var $input= $("<input>",{
                type:"text",
                name:"customerName",
                class:"form-control"
            });
            $("#conditionContent").append($input);
        }
        //时间段
        if(condition=="create_time"){
            var $input1= $("<input>",{
                type:"date",
                name:"startTime",
                class:"form-control"
            });
            var $input2= $("<input>",{
                type:"date",
                name:"endTime",
                class:"form-control"
            });
            $("#conditionContent").append($input1).append("----").append($input2);
        }
        //所属行业
        if(condition=="customer_industry"){
            var data = {

            };
            data.name = condition;
            $.ajax({
                type : 'post',
                url : 'CustomerServlet?method=getSomeCustomer',
                data : data,
                cache : false,
                sync : true,
                success : function(data) {
                   //alert(data);
                    var obj = JSON.parse(data);
                    //alert(obj);
                    var array = obj.data;
                    var $select=$("<select>",{
                        name:"fieldId",
                        class:"form-control"
                    });
                    $.each(array,function(index,ele){
                        //alert(ele.fieldId);
                        var $option=$("<option>",{
                            text:ele.fieldName,
                            value:ele.fieldId
                        });
                        $select.append($option);
                    });
                    $("#conditionContent").append($select);
                } 
            });
        }
        
        //-----------所属来源
        if(condition=="customer_source"){
            var data = {

            };
            data.name = condition;
            $.ajax({
                type : 'post',
                url : 'CustomerServlet?method=getSomeCustomer',
                data : data,
                cache : false,
                sync : true,
                success : function(data) {
                   //alert(data);
                    var obj = JSON.parse(data);
                    //alert(obj);
                    var array = obj.data;
                    var $select=$("<select>",{
                        name:"sourceId",
                        class:"form-control"
                    });
                    $.each(array,function(index,ele){
                        //alert(ele.sourceId);
                        var $option=$("<option>",{
                            text:ele.sourceName,
                            value:ele.sourceId
                        });
                        $select.append($option);
                    });
                    $("#conditionContent").append($select);
                } 
            });
        }
        
    });

3、当opiton为客户来源时发送一个ajax到后台,后台获取相关信息后返回一个List集合,将List用JSonArray和jsonObject进行处理转化为json字符串格式,返回前台请求,当请求成功后进行$each迭代获取list里面对象的属性,后台部分代码如下:

 1 List<WorkingField> list1 = new ArrayList<WorkingField>();
 2         JSONObject obj = new JSONObject();
 3         response.setCharacterEncoding("UTF-8");
 4         String condition = request.getParameter("name");
 5         try {
 6                 list2 = customerServiceI.getAllSource();
 7                 JSONArray jsonArray = JSONArray.fromObject(list2);
 8                 jsonArray.toString();
 9                 obj.put("data", jsonArray);
10 //                System.out.println(jsonArray);
11             
12         } catch (Exception e) {
13             e.printStackTrace();
14         }
15         try {
16             response.getWriter().write(obj.toString());
17         } catch (IOException e) {
18             e.printStackTrace();
19         }
20     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM