select2 - initselection错误

mar*_*kle 7 jquery jquery-select2

使用select2插件时出现以下错误initSelection.

Error: Uncaught Error: No select2/compat/initSelection 是我得到的错误.

希望有人帮我inistselection解决问题.

JS:

  $(".doctor_id_pat").select2({
    placeholder: "Search Users",
    minimumInputLength: 2,
    ajax: {
        url: "/listallergiesajax",
        dataType: 'json',
        data: function(term) {
            return {
                q: term
            };
        },
        results: function(data, page) {
            return {

                results: $.map(data, function(item) {

                    return {
                        text: item.text,
                        id: item.id
                    }
                })

            };
        },
    },
    initSelection: function(element, callback) {

        $.ajax("/listallergiesajax", {
            dataType: "json"
        }).done(function(data) {

            callback(data.results[0]);

        });
    }
});
Run Code Online (Sandbox Code Playgroud)

joo*_*ost 23

Select2 v3.5.2使用该initSelection方法.Select2 v4有不同的方法.

有关如何升级(initSelection以及其他内容)的信息,请参阅https://select2.github.io/announcements-4.0.html#removed-initselection.

  • 此信息已更改为[此链接](https://select2.org/upgrading/migrating-from-35#removed-the-requirement-of-initselection). (4认同)
  • @joost,我遇到了与 cr0ss 完全相同的问题,但这并不能通过仅引用 select2 资源来帮助我,因为它的文档记录很少。最好纠正他的问题中的 cr0ss 代码以使其正常工作。这样像我这样的其他人就可以看到工作代码的样子。希望你能看到这一点。谢谢! (2认同)