编程语言
首页 > 编程语言> > javascript – Select2 TypeError:b未定义

javascript – Select2 TypeError:b未定义

作者:互联网

enter image description hereenter image description here我正在使用select2来显示ajax结果下拉列表但是当我将数据附加到select2时它显示错误

TypeError: b is undefined

JS代码

        var baseurl = $("#baseurl").val();
        $(".myselect").select2({
            placeholder: "Select a inspector",
            allowClear: true,
            ajax: {
                url: baseurl + '/admin/getdata',
                dataType: 'json',
                type: "GET",
                quietMillis: 50,
                data: function (term) {
                    return {
                        term: term.term
                    };
                },
                results: function (data) {
                    var myResults = [];
                    $.each(data, function (index, item) {
                        myResults.push({
                            'id': item.id,
                            'text': item.firstname
                        });
                    });
                    return {
                        results: myResults
                    };
                }
            }
        });

term.term包含下拉搜索框中输入文本的值.

HTML

  <select class="myselect" style="width: 50% !important">
        <option></option>
        <option value="AL">Alabama</option>
        <option value="WY">Wyoming</option>
        <option value="KY">Kentucky</option>
  </select>

JSON响应

[{"id":9858,"firstname":"Testing3","status":2,"state":"VA","phone":""},{"id":9857,"firstname":"Testing2","status":2,"state":"VA","phone":""},{"id":9856,"firstname":" david polosky ","status":3,"state":"FL","phone":"(000)000-4141"}]

SELECT2 CDN链接

    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

PHP服务器代码(LARAVEL)

 $searchtext = $request->get('term');
 $data = Inspector::latest('id')
                ->select('id', 'firstname', 'status', 'state', 'phone')
                ->where('firstname', 'LIKE', '%' . $searchtext . '%')
                ->get()->toArray();
 echo json_encode($data);

任何帮助表示赞赏.

解决方法:

在您的ajax配置中,您使用应该使用processResults的结果

试试这个

var baseurl = $("#baseurl").val();
    $(".myselect").select2({
        placeholder: "Select a inspector",
        allowClear: true,
        ajax: {
            url: baseurl + '/admin/getdata',
            dataType: 'json',
            type: "GET",
            quietMillis: 50,
            data: function (term) {
                return {
                    term: term.term
                };
            },
            processResults: function (data) {
                var myResults = [];
                $.each(data, function (index, item) {
                    myResults.push({
                        'id': item.id,
                        'text': item.firstname
                    });
                });
                return {
                    results: myResults
                };
            }
        }
    });

标签:select2,javascript,php,jquery,laravel
来源: https://codeday.me/bug/20190727/1553778.html