【问题标题】:select2 : launch function when input changeselect2 : 当输入改变时启动函数
【发布时间】:2016-11-24 09:31:13
【问题描述】:

我使用 select2 标记从数据库中选择一组用户。 问题是用户数据库很大,我不想在使用 select2 时列出一千种可能性。

我希望仅当我在输入字段中键入时才更改可能性。类似于 ng-change,但 ng-change 仅在您在模型中添加或删除选项时才有效。

例如:我会在开始时有一个空列表,如果我输入“Mike”,我将拥有所有称为“Mike”的用户

<select2 multiple placeholder="Choose users" ng-model="usersSelection" s2-options="user.id as user.name for user in users"></select2>

你知道我该怎么做吗? select2 代码很大,我只是在寻找一个简单的解决方案,比如 watch 函数。

谢谢,对不起我的英语。

【问题讨论】:

  • 使用 jQuery 监听器怎么样,比如api.jquery.com/keypress
  • 不行,我想是因为select2标签,而不是简单的输入标签

标签: javascript angularjs select2 angularjs-select2


【解决方案1】:

我不知道angularjs,但是你不能在初始化select2框的时候使用属性minimumInputLength吗?

$("#element").select2({
    minimumInputLength: 3,
    ...
});

【讨论】:

  • Select2 使用maximumSelectionLength: 3,inputLength 只需要最少的输入字符。
  • 但这是一个不同的场景,不是吗?文档说,maximumSelectionLength 限制了所选项目的数量。 minimumInputLength 是一个设置,据我了解,何时开始查询。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2013-03-09
相关资源
最近更新 更多