简体   繁体   English

Select2仅更新一次禁用的选项

[英]Select2 updates disabled options only once

I have several select2 dropdown lists (containing the same options), and i need that each option could only be picked once. 我有几个select2下拉列表(包含相同的选项),我需要每个选项只能被选择一次。

Using select2 events system, each time a dropdown is opened, i go through the list and disable all the options that were selected on any of the other dropdowns 使用select2事件系统,每次打开下拉列表时,我都会遍历列表并禁用在其他任何下拉列表中选择的所有选项

$('body').on('select2:open', '.resCombo', function (event) {
    var $el = $(event.target);
    $el.find('option').removeAttr('disabled');
    $('.resCombo').not($el).find('option:selected').each(function (index,el) {
       console.log($(el).val());
       $el.find('option[value="'+ $(el).val() +'"]').attr('disabled','disabled');
     })
  $el.trigger('change.select2');
});

it works great the first time i change something at any of the dropdowns, but never on the second try. 第一次在任何下拉菜单中进行更改时,效果都很好,但是在第二次尝试中却从未更改过。

Select2 cache the disable property and don't listen to the changes of it. Select2缓存disable属性,并且不听其更改。 The quick fix for it is to initialize select2 each time you change the property. 快速解决方案是每次更改属性时初始化select2。 They have an open request for this feature, you can use this fix, although they haven't merged it yet. 他们对此功能有公开的要求,您可以使用此修复程序,尽管他们尚未将其合并。

Link to pull request 链接到拉取请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM