【问题标题】:Input field auto complete search select name and store the id in input box laravel输入字段自动完成搜索选择名称并将id存储在输入框laravel
【发布时间】:2019-10-25 12:21:52
【问题描述】:

input 字段 autocomplete 使用 laravel 和 jquery。我需要选择名称并将该用户的id 存储在input 字段中。

 <input id="show-user" type="text" class="typeahead form-control" name="student_code" value="{{ old('student_code') }}" placeholder="Student Code" required>

这里是javascript代码

var path = "{{ url('library/issue-books/autocomplete/') }}";
    $('input.typeahead').typeahead({
        source:  function (query, process) {
            return $.get(path, { query: query }, function (data) {
                return process(data);
            });
        }
    });

ptah 的控制器代码

$data = User::where("name","LIKE","%{$request->input('query')}%")->get();
    return response()->json($data);

现在我的问题是当我选择姓名时,需要在request 发送学生代码。 Student_code integer 专栏。

【问题讨论】:

    标签: javascript jquery mysql laravel typehead


    【解决方案1】:

    您可以将输入值发送到您的路线:

    return $.get(path + $('#show-user').val(), {}, function (data) {
        return process(data);
    });
    

    这样你应该有这样的路由语法:

    /library/issue-books/autocomplete/{$query}
    

    但你也可以只传递一个查询字符串:

    return $.get(path + '?query=' + $('#show-user').val(), {}, function (data) {
        return process(data);
    });
    

    【讨论】:

    • 感谢您的回复:)。此代码也像以前一样发送用户名,我需要所选用户的 id
    猜你喜欢
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2015-03-28
    相关资源
    最近更新 更多