依赖自动完成脚本

时间:2019-01-20 21:31:13

标签: javascript laravel

我正在尝试使用自动完成脚本,但是我遇到了这个问题

public function searchconcli(Request $request) {

    $query = $request->get('term', '');
    $cli_id = $request->get('id_cli', '');


    $countries = \DB::table('contactoscli')->where('cli_id', $cli_id);

    if ($request->type == 'cliente') {

        $countries->where('name', 'LIKE', '%' . $query . '%');

    }

好像变量$ cli_id一样为空,因为如果我手动设置值:

$cli_id = '4329';

脚本运行良好,并且仅显示具有此cli_id的结果

我使用javascript脚本发送name的值,但没有找到如何获取id_cli的值并使用ajax发送的

此javascript代码:

<script type="text/javascript">

//autocomplete script clientes
$(document).on('focus', '.autocompleteconcli_txt', function () {

    type = $(this).data('type');
    id_cli = document.getElementById("id_cli_1").value//obtener valor del
    if (type == 'cliente')
        autoType = 'name';

    $(this).autocomplete({
        minLength: 0,
        source: function (request, response) {
            $.ajax({
                url: "{{ route('searchajaxconcli') }}",
                dataType: "json",
                data: {
                    term: request.term,
                    type: type,
                    id_cli: request.id_cli,
                    type: type,
                },
                success: function (data) {
                    var array = $.map(data, function (item) {
                        return {
                            label: item[autoType],
                            value: item[autoType],
                            data: item
                        }
                    });
                    response(array)
                }
            });
        },
        autoFocus: true,
        minLength: 0,
        appendTo: "#crearviaje",
        select: function (event, ui) {
            var data = ui.item.data;
            id_arr = $(this).attr('id');
            id = id_arr.split("_");
            elementId = id[id.length - 1];
            $('#tel_con_cli').val(data.telefone);
            $('#name_con_cli').val(data.name);
            $('#mail_con_cli').val(data.mail);
            $('#id_con_cli').val(data.id);
        }
    });
});

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您的Ajax请求数据对象丢失id_cli

data: {
    id_cli: ???,
    term: request.term,
    type: type,
 }