jquery实现联动效果 (通用)省市的三级联动 四级联动 同样的做法

后台 数据格式(树结构 )Java构建树形结构 Cascader 级联选择器

数据结构

html

<div class="col-md-6">
     <select id="first-category">
         <option value="">请选择一级类别</option>
     </select>
     <select id="second-category">
          <option value="">请选择</option>
      </select>
      <select id="three-category">
          <option value="">---</option>
      </select>
 </div>

js categoriesValue 是后端传过来的数据

$(function () {
    console.log(categoriesValue)
    $.each(categoriesValue, function (index, obj) {
        let firstCategoryName = obj.label;
        let firstCategoryId = obj.id;
        // 将一级类别添加到select中
      $firstCategory.append(
            `<option value="${firstCategoryId}">${firstCategoryName}</option>`
        );
    })
  $firstCategory.change(function () {
        // 将二级类别的下拉列表设为空
      $secondCategory.empty();
      $secondCategory.append(`<option value="">---</option>`);
        // 获取一级类别所选择的类别文本信息
        let firstElementCategoryName = $(this).children("option:selected").text();
        $.each(categoriesValue, function (index, obj) {
            //获取一级类别名称
            let firstCategoryName = obj.label;
             //判断所选取的类别名称和类别名称是否一致
            if (firstElementCategoryName === firstCategoryName) {
                // 获取到二级类别数组
                let secondCategories = obj.children;
                // 遍历二级列数组
                $.each(secondCategories, function (index, obj) {
                    // 获取二级类别信息
                    let secondCategoryName = obj.label;
                    let secondCategoryId = obj.id;
                    // 将二级类别填写到select中
                  $secondCategory.append(
                        `<option value="${secondCategoryId}">${secondCategoryName}</option>`
                    );
                });
            }
        });
    });

  $secondCategory.change(function () {
        // 将三级类清空
        $threeCategory.empty();
        $threeCategory.append(`<option value="">---</option>`);
        //  获取所选择的二级类别
        let secondElementCategoryName = $(this).children("option:selected").text();
        $.each(categoriesValue, function (index, obj) {
            // 获取一级类别的数据
            let secondCategories = obj.children;
            $.each(secondCategories, function (index, cityObj) {
                //  二级类别的名称
                let secondCategoryName = cityObj.label;
                if (secondElementCategoryName === secondCategoryName) {
                    // 获取到三级类别的名称
                    let threeCategories = cityObj.children;
                    // 遍历三级类别
                    $.each(threeCategories, function (index, value) {
                        //三级类别名称填写到select中
                          let threeCategoryName= value.label;
                          let threeCategoryId = value.id;
                        // 将该城市对应的区级名称添加到对应的select元素中
                        $threeCategory.append(`<option value="${threeCategoryId}">${threeCategoryName}</option>`);
                    });
                }
            });
        });
    });
})

let $firstCategory = $("#first-category");
let $secondCategory = $("#second-category");
let $threeCategory = $("#three-category");

效果图效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值