向类型上的多个 Selectize 输入添加选项

Add options to multiple Selectize inputs on type

我正在一页中加载多个 Selectize select 输入,如下所示:

var selectizeInput = [];
$('.select-photo-id').each(function (i) {
    var selectedValue = $(this).val();
    selectizeInput[i + 1] = $(this).selectize({
        'maxOptions': 100,
        'items': [selectedValue],
        'onType': function (input) {
            $.post("admin/ajax/search_photos_for_postcards",
                {input: input},
                function (data) {
                    $(this).addOption(data);
                    $(this).refreshOptions();
                }, 'json');
        }
    });

});

事件 onType 进行了一个函数调用,returns 我希望立即在 Selectize 输入中提供新选项列表。有没有办法从那里调用 Selectize 实例?从代码中可以看出,我尝试使用 $(this) 访问它,但它失败了。我也试过 $(this).selectize,但它是一样的。正确的做法是什么?

我设法修复了它:

   'onType': function (input) {
        var $this = $(this);
        $.post("admin/ajax/search_photos_for_postcards",
            {input: input},
            function (data) {
                $this[0].addOption(data);
                $this[0].refreshOptions();
            }, 'json');
    }

您可能想使用 Selectize.js API 提供的 load 事件,如 the demos 中所示。滚动直到找到 "Remote Source — Github",然后单击其下方的 "Show Code"。