Bootstrap 2.2提前发行 [英] Bootstrap 2.2 Typeahead Issue

查看:66
本文介绍了Bootstrap 2.2提前发行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the following code to get suggestion list when I type on my text box.

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..">

现在,当我在文本框中键入内容时,我会得到错误

Now when I type on the text box I am getting the error

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

推荐答案

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

what is typeahead ? you obviously needs to do something with it before invoking the process member. ( instanciation , whatever typeahead is supposed to be ).

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指令).

in functional programming it is called continuation ( like a GOTO instruction ).

您不能确定回调是什么,回调是函数,因此除了对收到的数据进行调用外,不要尝试做其他任何事情.同样,回调是类似于GOTO的指令,它是一种延续,您无法控制它.您需要以数据作为参数来执行它.

you do not decide what callback is , callback is function so dont try to do anything else than calling it with the data you received. Again , callback is a GOTO like instruction , it is a continuation , you dont control it. you need to execute it with data as parameter.

这篇关于Bootstrap 2.2提前发行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆