Javascript 在下拉列表中单击选定项目时,防止Select2切换选定项目

Javascript 在下拉列表中单击选定项目时,防止Select2切换选定项目,javascript,jquery,select2,Javascript,Jquery,Select2,对多值选择框使用Select2()时,可以从下拉列表中切换选定值。有没有办法防止这种行为?因此,您只能通过单击选定项目旁边的X来删除这些项目 非常感谢所有帮助。您可以使用取消选择事件: $(".js-source-states").select2() .on("select2:unselecting", function (e) { // make sure we are on the list and not within input box if (e.params.a

对多值选择框使用Select2()时,可以从下拉列表中切换选定值。有没有办法防止这种行为?因此,您只能通过单击选定项目旁边的X来删除这些项目


非常感谢所有帮助。

您可以使用
取消选择事件:

$(".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();
    }
  }
);
普朗克:

有关详细信息,请参阅


如果要关闭选择,请使用
$example.select2(“关闭”)()

要查找此项,我想说的是,在选定选项后,只需从选项列表中删除选定的选项……谢谢各位,但在这种情况下,我们需要该项保留在下拉列表中,但不具有切换功能。