【问题标题】:Prevent Select2 from toggling selected items when clicking them in the dropdown防止 Select2 在下拉菜单中单击它们时切换所选项目
【发布时间】:2016-11-17 11:18:52
【问题描述】:

将 Select2 (https://select2.github.io/examples.html) 用于多值选择框时,您可以从下拉列表中切换所选值。有什么办法可以防止这种行为?这样您就只能通过单击它们旁边的 X 来删除选定的项目。

非常感谢所有帮助。

【问题讨论】:

  • 我想说的是在选择后从选项列表中删除选定的选项...
  • 谢谢大家,但在这种情况下,我们需要项目保留在下拉列表中,但没有切换功能。

标签: javascript jquery select2


【解决方案1】:

你可以使用unselecting事件:

$(".js-source-states").select2()
  .on("select2:unselecting", function (e) { 
    // make sure we are on the list and not within input box
    if (e.params.args.originalEvent.currentTarget.nodeName === 'LI') {
      e.preventDefault();
    }
  }
);

plunker:http://plnkr.co/edit/f8w97dSykPmbCZkN65JA?p=preview

查看https://select2.github.io/examples.html#programmatic-control了解更多详情,

如果要关闭选择,请使用$example.select2("close"); (https://select2.github.io/examples.html#programmatic)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 2018-02-16
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 2018-10-13
    • 2014-11-22
    相关资源
    最近更新 更多