0

テキストボックスに入力すると、次のコードを使用して候補リストを取得しています。

JS

$("#address").typeahead({
    source: function(query,typeahead){ 
        $.ajax({
            url: "http://localhost/disc/autocomplete/"+query,
            type: "GET",
            dataType: "JSON",
            async: true,
            success: function(data){
                typeahead.process(data); 
            }
        });
    },
    property: 'address',
    items:8,
    onselect: function (obj) { 
        // window.location = obj.url;
    }   
});

PHP

    $count=0;
    foreach ($query->result() as $row)
    {
        $count++;
        $item['value'] = $row->address;
        $item['id'] = $count;
        $output[] = $item;
    }        
    echo json_encode($output);

TextBox

<input type="text" id="address" autocomplete="off" name="address" class="input-block-level" placeholder="Street address..">

テキストボックスに入力すると、エラーが発生します

Uncaught TypeError: Object function (){return a.apply(c,e.concat(k.call(arguments)))} has no method 'process' 

編集:

$("#typeahead").typeahead({
    source: function(query,callback){ 
        $.ajax({
            url: "http://192.168.8.132/disc/autocomplete/"+query,
            type: "POST",
            dataType: "JSON",
            async: false,
            success: function(data){                   
                //this.process(data);
                callback(data);
            }
        });
    },
    items:8,
    onselect: function (obj) { 
    // window.location = obj.url;
    }   
});
4

1 に答える 1

2

タイプアヘッドとは何ですか?プロセスメンバーを呼び出す前に、明らかに何かをする必要があります。(インスタンス化、タイプアヘッドが想定されているものは何でも)。

編集1:

source: function(query,callback/** you need that to execute something after the XMLHttp request has returned**/){ 
        $.ajax({
            url: "http://localhost/disc/autocomplete/"+query,
            type: "GET",
            dataType: "JSON",
            async: true,
            success: function(data){
                /** execute the callback here do whatever data processing you want before**/
                callback(data); 
            }
        });
    },

関数型プログラミングでは、継続と呼ばれます ( GOTO 命令のように)。

編集2:

コールバックが何であるかを決定しないでください。コールバックは関数なので、受け取ったデータで呼び出す以外に何もしようとしないでください。繰り返しますが、コールバックは GOTO のような命令であり、継続であり、制御できません。データをパラメーターとして実行する必要があります。

于 2012-11-29T22:56:44.177 に答える