描述:
我正在使用Select2进行下拉,我正在尝试禁用列表中的选定选项。我写了下面给出的代码,但它没有用。这段代码有什么错误吗?
码:
$("#Users").change(function () {
$(".select2-results .select2-results__option[aria-selected=true]").each(function () {
$(this).attr("disabled","disabled");
});
});
HTML:
<div class="form-group">
@Html.Label("Send Mail To", new { @class = "control-label", @style= "position: absolute;margin-top:18px;" })
<a id="clearSelectedUsers" class="pull-right btn btn-primary M-B10">Clear</a>
@Html.DropDownListFor(m => m.Users, new SelectList(Model.Users, "Email", "Name"), "Select", new { multiple = "multiple", @class = "form-control" ,@disabled = "disabled" })
</div>
脚本:
var usersSelect2 = $(“#Users”)。select2(); usersSelect2.val(toBeSelectedManagers).trigger( “变化”);
尝试:1。如果要禁用Full select
元素
$("#Users").change(function () {
$("#Users").prop("disabled", true);
});
要么
$("#Users").change(function () {
$(this).prop("disabled", true);
});
2.如果你想禁用selected option
然后做类似的事情
$("#Users").change(function () {
$.each(this.options, function (i, item) {
if (item.selected) {
$(item).prop("disabled", true);
}else {
$(item).prop("disabled", false);
}
});
});
您可以在this链接中找到更多信息。
select2下拉列表下一次选择的值不能选择相同的值
$("#m_select2_2").select2()
.on("select2:select", function (e) {
var selected_element = $(e.currentTarget);
var select_val = selected_element.val();
$.each(this.options, function (i, item) {
if (item.selected) {
$(item).prop("disabled", true);
}
});
console.log(select_val);
$('select').select2();
});