0

我在互联网上找到了几个例子,但它们对我不起作用。我正在尝试对一个 jquery ui 自动完成框的选择做出反应。我设法对结果进行了有效的回调,但是当我单击一个条目时,不会触发选择事件。我的代码如下所示:

     select: function( event, ui ) 
     {
         alert( ui.item.value);
     }

响应来自 PHP 文件。我尝试使用仅包含结果名称的普通 JSON 数组,在互联网上进行一些研究后,我将其更改为如下所示的数组:

[{"id":0,"label":"Pinocchio","value":"Pinocchio"}]

但这对我也不起作用。那么它有什么问题呢?

[编辑]:在视图中:

$('#search').autocomplete
    ({
       source:
        function(request, response)
        {
        var input = $('#search').val();
        $.ajax(
        {
                type: 'POST',
                url: '".CController::createUrl('autoComplete')."',
                data:
                {
                    string: input
                },
                success:
                        function (data)
                        {
                            response(data);
                        },
                dataType: 'json',
                 select: function( event, ui ) {
                 alert( ui.item.value);
                }
        });
        }
    })

Yeah, the array looks like I posted above. But even if it looks like ["value1","value2"] the select event is not fired. The weird thing is that the results ARE displayed however a select has no effect. So either it's the select part of the autocomplete or the array has not the correct form. But does it need a certain form?

4

1 回答 1

1

The select is an event of autocomplete not ajax

$('#search').autocomplete ({
    source: function(request, response) {
        var input = $('#search').val();
        $.ajax(  {
            type: 'POST',
            url: '".CController::createUrl('autoComplete')."',
            data:{
                    string: input
            },
            success: function (data) {
                response(data);
            },
            dataType: 'json'
        });
    },
    select: function( event, ui ) {  //<<---- This should be passed to the `autocomplete` configuration
        alert( ui.item.value);
    }
})
于 2013-02-23T13:46:57.417 回答