简体   繁体   English

禁用所选选项后,select2不更新

[英]select2 is not updated after disable of selected option

I'm trying to disable the selected option in select2 but it seems like the select2 is not refreshing. 我正在尝试禁用select2中的选定选项,但似乎select2没有刷新。

My purpose is to disable each item in the select2 list after selecting it. 我的目的是在选择它之后禁用select2列表中的每个项目。 I see the HTML that the option got the disabled attribute but the option is still enabled in the select2 element. 我看到该选项具有禁用属性的HTML,但是在select2元素中该选项仍处于启用状态。

$("#select-filter").select2({
    placeholder: "Select a category",
    data:[{
        id: "",
        text: ""
    },{
        id: "project",
        text: "project"
    },{
        id: "date",
        text: "date"
    },{
        id: "user",
        text: "user"
    }]
}).on("change", function(e) {
    $("#select-filter :selected").attr("disabled", "true");
});

See example : https://jsfiddle.net/bkqeqbay/1/ 参见示例: https : //jsfiddle.net/bkqeqbay/1/

You can use the select event and the data like so: 您可以使用select事件和数据,如下所示:

...}).on("select2:select", function(e) {
  console.dir(e.params.data);
  e.params.data.disabled = true;
});

Updated fiddle: https://jsfiddle.net/bkqeqbay/4/ 更新的小提琴: https : //jsfiddle.net/bkqeqbay/4/

Note that if you must also disable the underlying select element option you can do: 请注意,如果还必须禁用基础的select元素选项,则可以执行以下操作:

 var index = $(e.params.data.element).index();
 $("#select-filter").find('option').eq(index).prop('disabled',true);

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

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