简体   繁体   English

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

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

I've changed the standard method of Jquery UI autocomplete to use POST instead of GET: 我将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
    });
});

But "data" is always empty, no matter what I write in the input field. 但是,无论我在输入字段中写什么,“数据”始终为空。

I'm using an external file called ajax.php to get all saved locations from a database, but it'll show always all locations available, because "data" doesn't read what I'm typing in the input field. 我正在使用一个名为ajax.php的外部文件来从数据库获取所有保存的位置,但是它将始终显示所有可用的位置,因为“数据”不会读取我在输入字段中键入的内容。

I really appreciate any help! 我非常感谢您的帮助!

The variable data is not updated when you change the value of your input field, but only once: when the document is loaded. 更改输入字段的值时,变量数据不会更新,而只会更新一次:加载文档时。

Use this instead of your source option: 使用它代替源选项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM