【问题标题】:Bootstrap 2.2 Typeahead IssueBootstrap 2.2 Typeahead 问题
【发布时间】:2012-11-18 03:14:17
【问题描述】:

当我在文本框中输入内容时,我正在使用以下代码获取建议列表。

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;
    }   
});

【问题讨论】:

    标签: php javascript jquery twitter-bootstrap


    【解决方案1】:

    什么是预输入?您显然需要在调用流程成员之前对其进行处理。 (实例化,无论预输入应该是什么)。

    编辑 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:

    你不决定什么是回调,回调是函数,所以除了用你收到的数据调用它之外,不要尝试做任何其他事情。同样,callback 是一个类似 GOTO 的指令,它是一个延续,你不能控制它。您需要使用数据作为参数来执行它。

    【讨论】:

    • 如果你明白我的意思,typeaheadBootstrap 的一个特征。我必须实例化 tat 吗?如果是,我该怎么做?
    • 我知道什么是 Twitter 引导程序,什么是提前输入,我说的是源回调和你传递给源函数的提前输入对象。这是什么?
    • 显然我当时不明白那部分。我没有将任何东西传递给回调。你能告诉我在那里做什么吗?实际上我的代码看起来就像现在一样。我没有隐藏任何东西..
    • 所以我需要调用typehead.process(data) 我应该把那行放在回调函数中吗?
    • 不,您不需要调用 typehead.process(data) ,您需要调用在源函数中定义为参数的回调。如果您想在此之前进行一些数据处理,请在 ajax 回调中或您想要的任何地方进行,但直接调用函数变量,因为它在源函数签名中定义,只需查看我的代码,不难理解什么我正在做。回调是一个函数。
    猜你喜欢
    • 1970-01-01
    • 2013-08-12
    • 2014-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多