【问题标题】:Select2 minimumInputLength Not Working After append()Select2 minimumInputLength 在 append() 之后不起作用
【发布时间】:2015-04-24 13:03:27
【问题描述】:

在我的 select2 中,我使用 minimumInputLength 选项在一定长度后开始过滤。

selectExample.select2({
  minimumInputLength: 2
});

但是在使用 append() 函数动态追加新选项后,这个 minimumInputLength 选项不起作用。

selectExample.append('<option value="10">Ten</option>');

究竟发生了什么?是否有特定的方法可以为 select2 添加新选项?

我当前使用的解决方案是重新声明 .select2() 并包含 minimumInputLength 选项。

【问题讨论】:

标签: javascript jquery jquery-select2


【解决方案1】:
var dynamicOptions=[{ id: 0, text: 'enhancement' }, { id: 1, text: 'bug' }, { id: 2, text: 'duplicate' }, { id: 3, text: 'invalid' }, { id: 4, text: 'wontfix' }];

 $("#select2").select2('data', dynamicOptions);

这就是您需要添加选项的方式

【讨论】:

  • 如何为选项html添加属性?例如,我想在 select 选项中添加 data-filename 属性。
  • 试过你的解决方案,仍然需要用 minimumInputLength 重新声明 select2。
猜你喜欢
  • 2015-01-18
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
相关资源
最近更新 更多