首页 > 解决方案 > Select2 版本 4 及更高版本无法获取数据结果

问题描述

我有这个下面的javaScript,它在Select2版本低于 4 时运行良好,所以,现在我要将它升级到 4.0.7 版本,但它不起作用。你能指导我如何解决它吗?

    $(".locations").select2({
        minimumInputLength: 3,
        width: '100%',
        maximumSelectionSize: 1,
        ajax:{
            url: "My_URL_HERE",
            dataType: 'json',
            data: function(term) {
                return { q:term }
            },
            results:function(data) {
                return { results: data }
            }
        }
    });

        $(".locations").on("select2-selecting",function(e){
        $("input[name=module_type]").val(e.object.module);
        $("input[name=select2-search__field]").val(e.object.id);
        $("input[name=slug]").val(e.object.id);
        });

这是我的输入代码:

    <input type="text" name="name" class="locations">

提前致谢

标签: javascriptjsonajaxjquery-select2

解决方案


你应该使用select而不是input

<select name="name" class="locations"></select>

阅读更多:Select2 Ajax(远程数据)


推荐阅读