无法在 jquery 自动完成中获取所选项目

Cannot get selected item in jquery autocomplete

使用 Jquery 自动完成 UI 小部件,我有以下代码通过外部 php 获取 suburbs/postcodes 列表:

    <script>

    $(function() {
        $("#suburb").autocomplete({

            minLength:3, //minimum length of characters for type ahead to begin
            source: function (request, response) {
                $.ajax({
                    type: 'POST',
                    url: 'resources/php/helpers.php', //your server side script
                    dataType: 'json',
                    data: {
                        suburb: request.term
                    },

                    success: function (data) {
                        //if multiple results are returned
                        if(data.localities.locality instanceof Array)
                            response ($.map(data.localities.locality, function (item) {
                                return {
                                    label: item.location + ', ' + item.postcode,
                                    value: item.location + ', ' + item.postcode
                                }
                            }));
                        //if a single result is returned
                        else
                            response ($.map(data.localities, function (item) {
                                return {
                                    label: item.location + ', ' + item.postcode,
                                    value: item.location + ', ' + item.postcode
                                }
                            }));
                    },

                    select: function (event, ui) {
                        alert("SELECT");
                        $('#postCode').val("POSTCODE");
                        return true;
                    }
                });
            }

        });
    });
</script>

自动完成本身运行良好,我得到了匹配列表,但是 'select' 部分不起作用,即,我需要将另一个输入文本值设置为所选值,但即使在上面代码,警报对话框不会被调用 - 我看到的各种语法让我有点困惑,所以我不确定我在这里做错了什么。

select函数应该在发送给ajax方法的对象之外。

试试这个:

$(function() {
    $("#suburb").autocomplete({

        minLength:3, //minimum length of characters for type ahead to begin
        source: function (request, response) {
            $.ajax({
                type: 'POST',
                url: 'resources/php/helpers.php', //your server side script
                dataType: 'json',
                data: {
                    suburb: request.term
                },

                success: function (data) {
                    //if multiple results are returned
                    if(data.localities.locality instanceof Array)
                        response ($.map(data.localities.locality, function (item) {
                            return {
                                label: item.location + ', ' + item.postcode,
                                value: item.location + ', ' + item.postcode
                            }
                        }));
                    //if a single result is returned
                    else
                        response ($.map(data.localities, function (item) {
                            return {
                                label: item.location + ', ' + item.postcode,
                                value: item.location + ', ' + item.postcode
                            }
                        }));
                }
            });
        }, 

        select: function (event, ui) {
            alert("SELECT");
            $('#postCode').val("POSTCODE");
            return true;
        }            

    });
});