You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用jQuery自动选中multi-select picker多选下拉框的指定选项

实现方案

你可以直接通过jQuery给多选框赋值数组,再刷新selectpicker组件实现自动选中,完整代码如下:

// 1. 获取待选中的字符串
var operation_day = editData.attr('data-operation_day');
// 2. 拆分逗号分隔的字符串为数组,统一转小写适配option的value大小写,避免匹配失败
var selectedDays = operation_day.split(',').map(day => day.trim().toLowerCase());
// 3. 直接给多选select赋值数组,自动匹配选中对应option
$('select[name="operation_day[]"]').val(selectedDays);
// 4. 刷新kt-selectpicker组件,同步渲染界面的选中状态(必不可少)
$('select.kt-selectpicker').selectpicker('refresh');

注意事项

  • 如果你能保证接口返回的operation_day值和option的value大小写完全一致,可以去掉.toLowerCase()的转换逻辑
  • 必须调用selectpicker('refresh')方法,否则手动修改DOM属性不会同步到selectpicker的渲染界面
  • 异步ajax场景下需要确保这段代码在selectpicker组件初始化完成、ajax请求返回后再执行

内容的提问来源于stack exchange,提问作者Bilal Arshad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.20 01:15:03