当我在文本框中键入内容时,我使用以下代码来获取建议列表。
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);
文本框
<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;
}
});
最佳答案
什么是预输入?在调用进程成员之前,您显然需要对其执行一些操作。 (实例化,无论预输入应该是什么)。
编辑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的指令,它是一个延续,你无法控制它。您需要使用数据作为参数来执行它。
关于php - Bootstrap 2.2 Typeahead 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13636088/