【发布时间】:2015-02-10 14:38:48
【问题描述】:
我正在开发一个使用 Select2 插件的应用程序。我需要允许用户在框中键入以显示选项供他们选择。我有那个工作。但是,我还想让用户能够在列表中输入新选项。我不知道该怎么做。目前,我的 Select2 初始化如下所示:
$('#myField').select2({
allowClear: true,
placeholder: 'Please select',
minimumInputLength: 3,
initSelection: function (element, callback) {
var id = $(element).val();
if (id) {
callback({ ItemId: id, ItemText: selectedName });
}
},
ajax: {
url: '/api/suggestions',
dataType: 'json',
quietMillis: 150,
data: function (term, page) {
return {
q: term,
count: 3
}
},
results: function (data, page) {
return { results: data, id: 'ItemId', text: 'ItemText' };
}
},
id: function (item) { return item.ItemId; },
text: function (item) { return item.ItemText; },
formatResult: function (i) { return i.ItemText },
formatSelection: function (i) { return i.ItemText }
});
【问题讨论】:
标签: jquery jquery-select2