无法 select 新添加的 selectize 组件 post 一个 ajax 调用

Unable to select the newly added selectize components post an ajax call

无法select新添加的selectize组件postajax调用

我已经使用当前 selected 选项预先填充了选项,并尝试通过来自服务器的 ajax 调用来检索更多选项。 我的服务器 returns 数据是这样的,但我不确定如何处理这些数据

[{"id":303,"name":"Short Sleeve Opening"}]

我试过使用 addOption 和 refreshOptions 方法,但它们似乎不起作用。 下面是调用 selectize 组件的代码。

$(function () {
    $select_options = $('select').selectize({
        plugins: ['restore_on_backspace', 'remove_button', 'drag_drop'],
        delimiter: ',',
        persist: false,
        create: function (input) { // I am not sure if this is of any use, as I never found the control to enter in this part of  code during debugging.
            return {
                value: input,
                text: input
            }
        },
        load: function (query, callback) {
            if (!query.length) return callback();
            $.ajax({
                dataType: 'json',
                url: '/measurement_tags/autocomplete_key?q=' + encodeURIComponent(query),
                error: function () {
                    callback();     //Not sure what the callback should be, documenation is not self explanatory
                },
                success: function (res) {
                //Added this callback to add the new options to the list, it is adding the new options to the list but I can't select them at all
                //Moreover, second time I try to use autocomplete it doesn't work. The control doesn't reach the load method, and no error observed in console
                    for (index in res) {
                        $('.selectize-dropdown-content').append('<div class="option" data-selectable="" data-value="' + res[index].id + ' ">' + res[index].name + '</div>');
                    }
                }
            });
        }
    });
});

将新选项永久添加到列表的确切方法是什么?

浪费了一整天终于实现了:

$(function () {
    $select_options = $('select').selectize({
        plugins: ['restore_on_backspace', 'remove_button', 'drag_drop'],
        delimiter: ',',
        persist: false,
        options: [],
        load: function (query) {
            $.ajax({
                dataType: 'json',
                url: '/measurement_tags/autocomplete_key?q=' + encodeURIComponent(query),
                success: function (res) {
                    updateOptions(res);
                }
            });
        }
    });
    var updateOptions = function (newOptions) {
        var selectize = $select_options[0].selectize;
        for (index in newOptions) {
            selectize.addOption({
                value: newOptions[index].id,
                text: newOptions[index].name,
            });
        }
        selectize.refreshOptions();
    }
});