Javascript 用于typeahead的更新功能
我想从服务器获取数据,并建议使用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({
/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'
});
或
本地数据