在下拉列表中单击时,阻止Select2切换所选项目

nik*_*oka 2 javascript jquery select2

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

所有帮助非常感谢.

And*_*riy 6

你可以使用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();
    }
  }
);
Run Code Online (Sandbox Code Playgroud)

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)