Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用于typeahead的更新功能_Javascript_Twitter Bootstrap_Typeahead - Fatal编程技术网

Javascript 用于typeahead的更新功能

Javascript 用于typeahead的更新功能,javascript,twitter-bootstrap,typeahead,Javascript,Twitter Bootstrap,Typeahead,我想从服务器获取数据,并建议使用typeahead。 我从列表中获取数据到一个类似于/foo/q=QUERY的usl,这个查询返回类似于[“salam”,“bye”,“khodafez,]的json。 如何使用从引导类型到建议。 我尝试以下代码: <script type="text/javascript"> $(document).ready(function() { $("#vahid").typeahead({

我想从服务器获取数据,并建议使用typeahead。 我从列表中获取数据到一个类似于
/foo/q=QUERY
的usl,这个查询返回类似于
[“salam”,“bye”,“khodafez,]
的json。 如何使用从引导类型到建议。 我尝试以下代码:

 <script type="text/javascript">
            $(document).ready(function() {
                $("#vahid").typeahead({
                     // ???    
                });
            });
          </script>

$(文档).ready(函数(){
$(“#vahid”)。提前键入({
// ???    
});
});
根据,您只需在选项中为
源提供一个函数:

$("#vahid").typeahead({
    source: function(query, process) {
        // `query` is the text in the field
        // `process` is a function to call back with the array
        $.ajax({
            // Your URL
            url: "/foo",

            // Your argument with the query as its value
            data: {q: query},

            // Success callback -- since it expects the first
            // parameter to be the data, and that's what the
            // success callback is called with, you can just
            // use `process` directly
            success: process
        });
    }
});
以上假设响应正确地将响应标识为
内容类型:application/json

dataType: "json"
…到您的
ajax
选项

我之所以使用异步机制,是因为您正在查询服务器。如果您已经有了数据客户端,您可以直接从
函数返回数据。但是它们(智能地)不要求您从服务器同步检索它,因为这会导致错误的XU。

根据,您只需在选项中为
源提供一个函数:

$("#vahid").typeahead({
    source: function(query, process) {
        // `query` is the text in the field
        // `process` is a function to call back with the array
        $.ajax({
            // Your URL
            url: "/foo",

            // Your argument with the query as its value
            data: {q: query},

            // Success callback -- since it expects the first
            // parameter to be the data, and that's what the
            // success callback is called with, you can just
            // use `process` directly
            success: process
        });
    }
});
以上假设响应正确地将响应标识为
内容类型:application/json

dataType: "json"
…到您的
ajax
选项

我之所以使用异步机制,是因为您正在查询服务器。如果您已经拥有数据客户端,您可以直接从
源代码
函数返回数据。但是它们(智能地)不要求您从服务器同步检索数据,因为这会导致错误的XU。

根据文档 您可以添加ajax属性,如:

$('#myElement').typeahead({
    ajax: '/path/to/mySource'
});

对于本地数据,请参见文档 您可以添加ajax属性,如:

$('#myElement').typeahead({
    ajax: '/path/to/mySource'
});

本地数据