Jeesite下拉框的二级联动

3 篇文章 0 订阅

话不多说 首先上图片
这里写图片描述
JSP 页面代码 截图

第一个下拉框JSP代码

        <div class="control-group">
            <label class="control-label">离职类型:</label>
            <div class="controls">
                <form:select id="type" name="type" path="type" class="input-xlarge" onchange="findtemplate(this.options[this.options.selectedIndex].value);"
                    style="width:180px;">
<%--                    <form:option value="" label="" /> --%>
                    <form:options items="${fns:getDictList('upl_type')}"
                        itemLabel="label" itemValue="value" htmlEscape="false" />
                </form:select>
            </div>
        </div>

第二个下拉框JSP代码

        <div class="control-group">
            <label class="control-label">选择模板:</label>
            <div class="controls">
                <form:select id="template" name="template" path="type" class="input-xlarge"
                    style="width:180px;">
<%--                    <form:option value="" label="" /> --%>
                    <form:options id="wordTemplate" items="${fns:getDictList('upc_word')}"
                        itemLabel="label" itemValue="value" htmlEscape="false" />
                </form:select>
                <%--                <a href="${ctx}/sys/userdetail/upcform">导出模板</a> --%>
            </div>
        </div>

精髓部分为 第一个下拉框

onchange="findtemplate(this.options[this.options.selectedIndex].value);"

说明:findtemplate()为函数名称
this.options[this.options.selectedIndex].value 这句话能获取到第一个下拉框选择的值

JS代码部分:

function findtemplate(v) {
    $.ajax({
        type : "post",
        async : false,
        url : "getPostTemplateJson",
        data : {
            'type' : v
        },
        dataType : "json",
        success : function(msg) {
            $("#template").empty();
            //$("#template").select2();
            if (msg.length > 0) {
                for (var i = 0; i < msg.length; i++) {
                        var partId = msg[i].value;
                        var partName = msg[i].label;
                        var $option = $("<option>").attr({
                            "value" : partId
                        }).text(partName);
                        $("#template").append($option);
                }
                //$("#template option:first").prop("selected", 'selected');
                $("#template").change();

            }
        },
        error : function(json) {
            $.jBox.alert("网络异常!");
        }
    });
}

JS代码说明:
通过AJAX请求 后台数据 然后采取循环拼接的办法 构造下拉框的。 option。。
必要说明:$(“#下拉框”).empty(),为 jquey写法,必写。

$(“#下拉框”).change(); 这个change()函数 一定要写,否则有一个小bug。
这个bug是什么就不明说了,复制代码的兄弟 可以去掉试试效果。

这是个很细节的操作哦 。。。。。。。。。。。。。。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
下拉框二级联动数据库的实现需要前端和后端的配合,前端需要通过JavaScript监听第一个下拉框的变化事件,然后向后端发送Ajax请求获取第二个下拉框的数据。后端需要提供两个接口,一个用于获取第一个下拉框的数据,另一个用于根据第一个下拉框择值获取第二个下拉框的数据。 以下是一个简单的实现思路: 1.前端代码: ```html <!-- 第一个下拉框 --> <select id="building"> <option value="">请择公寓</option> <!-- 动态添加项 --> </select> <!-- 第二个下拉框 --> <select id="dormitory"> <option value="">请择宿舍</option> <!-- 动态添加项 --> </select> <script> // 监听第一个下拉框的变化事件 document.getElementById('building').addEventListener('change', function() { // 获取择的值 var buildingId = this.value; // 发送Ajax请求获取第二个下拉框的数据 var xhr = new XMLHttpRequest(); xhr.open('GET', '/findAllDormitory?buildingId=' + buildingId); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { // 解析返回的数据 var dormitories = JSON.parse(xhr.responseText); // 清空第二个下拉框项 var dormitorySelect = document.getElementById('dormitory'); dormitorySelect.innerHTML = '<option value="">请择宿舍</option>'; // 动态添加项 dormitories.forEach(function(dormitory) { var option = document.createElement('option'); option.value = dormitory.dormitoryId; option.textContent = dormitory.dormitoryName; dormitorySelect.appendChild(option); }); } }; xhr.send(); }); </script> ``` 2.后端代码: ```python # 定义两个接口 @app.route('/findAllBuilding') def find_all_building(): # 查询公寓表获取数据 buildings = Building.query.all() # 将数据转换为字典列表 building_list = [{'buildingId': building.buildingId, 'buildingName': building.buildingName} for building in buildings] # 返回数据 return jsonify(building_list) @app.route('/findAllDormitory') def find_all_dormitory(): # 获取一级下拉框择值 building_id = request.args.get('buildingId') # 根据择值查询宿舍表获取数据 dormitories = Dormitory.query.filter_by(buildingId=building_id).all() # 将数据转换为字典列表 dormitory_list = [{'dormitoryId': dormitory.dormitoryId, 'dormitoryName': dormitory.dormitoryName} for dormitory in dormitories] # 返回数据 return jsonify(dormitory_list) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值