select2 option 设置disabled以及如何取消disabled属性

问题: 需要对某一个option设置为不可选,但是一些操作之后恢复可选状态。

//获取所有的要操作的option
var m_field_dom = Array.from($(".m-field").children("option"));

m_field_dom.forEach(item=>{
    var type = $(item).attr("data-type");
    $(item).prop("disabled",false);
    if(val == "sum"){
        if(type == "num"||type == "decimal"){
            $(item).prop("disabled",true);
        };
     }
});

关键一步:必须在改变disabled状态之后,对select重新select2
$(".m-field").select2();    
原文地址:https://www.cnblogs.com/fyjz/p/14844316.html