[英]Clear all of the list items of Select2 rather than the selected
I have a cascade Select2 ddl and when I select the master ddl, I populate the Detail ddl without no problem. 我有一个级联Select2 ddl,当选择主ddl时,可以毫无问题地填充Detail ddl。 On the other hand, when I select another item on master and then click the detail, at the first time the detail ddl lists the previous items just a miliseconds. 另一方面,当我在master上选择另一个项目然后单击详细信息时,详细信息ddl第一次会以毫秒为单位列出先前的项目。 So, I need to clear all of the list items besides the selected item when the main ddl's selected index changed. 因此,当主ddl的所选索引发生更改时,我需要清除除所选项目之外的所有列表项。 Is it possible? 可能吗? I have tried to all of the solution methods below, but they only clear the selected item. 我已经尝试了以下所有解决方法,但是它们仅清除所选的项目。 Any idea? 任何想法?
$('#ProjectId').select2('data', null);
$('#ProjectId').select2('data', { id: null, text: null })
$('#ProjectId').empty();
$('#ProjectId').val(null).trigger("change");
$("#ProjectId").remove();
$('#ProjectId').val('').trigger('change');
@Html.DropDownListFor(m => m.ProjectId, Enumerable.Empty<SelectListItem>(), "Select")
$(document).ready(function () {
var issueType = $("#ProjectId");
issueType.select2({
allowClear: true,
ajax: {
url: '/Controller/GetProjects',
dataType: 'json',
delay: 250,
data: function (params) {
return {
query: params.term, //search term
page: params.page,
id: selectedMasterId
};
},
processResults: function (data, page) {
var newData = [];
$.each(data, function (index, item) {
newData.push({
id: item.Id,
text: item.Description
});
});
return { results: newData };
},
cache: true
},
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
});
});
$('#MasterId').change(function () {
selectedMasterId = $(this).val();
$('#ProjectId').select2('val', '');
$('#ProjectId').select2('data', null);
}
});
$('#id').empty().trigger("change");
其中“ #id”是select2元素的jQuery选择器。
On the select element put onchange="removeOthers(this)"
in js code write this function: 在select元素上,在js代码中放置onchange="removeOthers(this)"
,编写以下函数:
function removeOthers(that){
$('option', that).not(':eq(0), :selected').remove();
//now refresh your select2
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.