select2在f12控制台赋值失败

本文探讨了在使用Select2控件时遇到的定位错误问题,详细解释了如何正确为放置在modal中的元素赋值,并介绍了select2的基本知识点,包括引入资源、实现多选功能以及触发change事件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#问题:如题
先说结果:定位出错!
$(’#employeeType’).val(‘3’).select2()——》失败
$(’#formAdd #employeeType’).val(‘2’).select2()——》成功
查看值:
$(’#employeeType’).val() “3”
$(’#formAdd #employeeType’).val() “2”

因为employeeType元素是放在modal中!

#select2的知识点

首先需要引入,select2.css,jquery,boostrap.js,select2.js

多选:html添加属性 multiple=“multiple”

百度到一个知识:赋值后没有变化,可能是没有调用change的问题
$(’#formAdd #employeeType’).val(‘2’).select2()
$(’#employeeType’).val(‘1’).trigger(“change”);// 静态数据

获得id:KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲customType').va…(’#customType’).val(‘1’).select2() 1就是id
设置id为空:KaTeX parse error: Expected 'EOF', got '#' at position 3: ('#̲customType').va…("#customType").select2(“data”,{id:‘1’,text:“员工”}) text就是要显示的。

// 加载树形数据 const load = async () => { try { const res = await request.get(‘/category/tree’) if (res.data.code === 200) { tableData.value = res.data } else { ElMessage.error(‘数据加载失败’) } } catch (error) { ElMessage.error(‘请求失败,请检查网络’) } }F12键检查发现{code: "200", msg: "请求成功",…} code : "200" data : [{id: 1, title: "前端开发", parentId: null, sort: 0,…}, {id: 2, title: "后端开放", parentId: null, sort: 0,…},…] 0 : {id: 1, title: "前端开发", parentId: null, sort: 0,…} children : [{id: 4, title: "Vue", parentId: 1, sort: 1, children: null},…] 0 : {id: 4, title: "Vue", parentId: 1, sort: 1, children: null} children : null id : 4 parentId : 1 sort : 1 title : "Vue" 1 : {id: 5, title: "JavaScript", parentId: 1, sort: 1, children: null} children : null id : 5 parentId : 1 sort : 1 title : "JavaScript" 2 : {id: 6, title: "JQuery", parentId: 1, sort: 1, children: null} children : null id : 6 parentId : 1 sort : 1 title : "JQuery" id : 1 parentId : null sort : 0 title : "前端开发" 1 : {id: 2, title: "后端开放", parentId: null, sort: 0,…} children : [{id: 7, title: "Java", parentId: 2, sort: 2, children: null},…] 0 : {id: 7, title: "Java", parentId: 2, sort: 2, children: null} children : null id : 7 parentId : 2 sort : 2 title : "Java" 1 : {id: 8, title: "C++", parentId: 2, sort: 2, children: null} children : null id : 8 parentId : 2 sort : 2 title : "C++" 2 : {id: 9, title: "Python", parentId: 2, sort: 2, children: null} children : null id : 9 parentId : 2 sort : 2 title : "Python" id : 2 parentId : null sort : 0 title : "后端开放" 2 : {id: 3, title: "数据库", parentId: null, sort: 0,…} children : [{id: 10, title: "MySQL", parentId: 3, sort: 3, children: null}] id : 3 parentId : null sort : 0 title : "数据库" msg : "请求成功"但是出现数据加载失败,而且页面也有展示数据.已经知道后端 @GetMapping("/tree") public Result getCategoryTree() { List<Category> treeData = categoryService.getCategoryTree(); return Result.success( treeData); }public List<Category> getCategoryTree() { List<Category> allCategories = categoryMapper.selectAll(); Map<Integer, Integer> idIndexMap = new HashMap<>(); // 建立ID到列表索引的映射 for (int i = 0; i < allCategories.size(); i++) { idIndexMap.put(allCategories.get(i).getId(), i); } // 构建树形结构 for (Category node : allCategories) { if (node.getParentId() != null) { Integer parentIndex = idIndexMap.get(node.getParentId()); if (parentIndex != null) { Category parent = allCategories.get(parentIndex); if (parent.getChildren() == null) { parent.setChildren(new ArrayList<>()); } parent.getChildren().add(node); } } } // 过滤出根节点 return allCategories.stream() .filter(c -> c.getParentId() == null) .sorted(Comparator.comparingInt(Category::getSort)) .collect(Collectors.toList()); } 怎么成功展示数据
最新发布
03-22
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值