select2使用不久,最近遇到一个问题,需要对某一个option设置为不可选,但是一些操作之后恢复可选状态。
1.设置为不可选状态,很容易,
$("#single_select option[value='b']").prop('disabled',true);
$("#single_select").select2();
2.取消option的disabled属性。
2.1 $("#single_select option[value='b']").prop('disabled',false);
2.2$("#single_select option[value='b']").removeProp('disabled');
$("#single_select").select2();
但是这样只能给option设置disabled,,取消disabled并没有生效。
解决方法:
每次更改了option的disabled属性时候,都需要再次调用该元素的select2()。