【问题标题】:Select2 version 4 and above not working to get data resultSelect2 版本 4 及更高版本无法获取数据结果
【发布时间】:2019-05-05 10:21:19
【问题描述】:

我有以下 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">

提前致谢

【问题讨论】:

    标签: javascript json ajax jquery-select2


    【解决方案1】:

    你应该使用select 而不是input:

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

    阅读更多:Select2 Ajax (remote data)

    【讨论】:

    • 我使用了 select 但没有找到结果。正如我所提到的,在以前的 Select2 版本中,它运行良好。
    • 试试processResults:function(data)...而不是results:function(data)...
    • 我也测试过,不行,processResults:function(data) { return { results: data } }
    猜你喜欢
    • 1970-01-01
    • 2011-11-03
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多