JQuery UI 自动完成 - 无法获取输入字段中键入的数据

JQuery UI AutoComplete - can't get data typed in the input field

本文关键字:字段 输入 数据 获取 UI JQuery      更新时间:2023-09-26

我已经将Jquery UI自动完成的标准方法更改为使用POST而不是GET:

$(function () {
    var data = $(this).val();
    var dataString = 'location=' + data;
    $(".suggest_locations").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                url: "ajax.php",
                data: dataString,
                dataType: "json",
                cache: false,
                success: response
            })
        },
        minLength: 1,
        delay: 0,
        autoFocus: true
    });
});

但是"数据"总是空的,无论我在输入字段中写什么。

我正在使用一个名为 ajax.php 的外部文件从数据库中获取所有保存的位置,但它将始终显示所有可用位置,因为"数据"不会读取我在输入字段中键入的内容。

我真的很感激任何帮助!

更改输入字段的值时,变量数据不会更新,而只会更新一次:加载文档时。

使用此选项代替源选项:

source: function(request, response) {
    $.ajax({
        url : 'ajax.php',
        type    : 'post',
        dataType: 'json',
        data    : 'location=' + request.term,
        cache   : false,
        success : function(data) {
            response(data);
        }
    });
}