我echo(json_encode($data));
从php返回一个JSON编码的数组:我希望它从JQuery自动完成功能中填充建议框。我正在使用这个:
$("#field").autocomplete({
source : "SearchTest.php",
maxLength: 5
});
不知道为什么这不起作用。每次按键后,我都会检索数据并在建议框中填充该数据,我不希望自动完成功能为我排序和选择,而是在服务器端进行。现在只是一个字符串列表。能够自定义数据的呈现方式也是很好的。
编辑:将源更改为发布:
$("#field").autocomplete({
source : function(request, response) {
$.post("SearchTest.php", request, response);
},
maxLength : 5
});
立即收到此错误:
Uncaught TypeError: Cannot use 'in' operator to search for '1240' in
Notice: Undefined index: field in /.../SearchTest.php on line 25
第25行是: $whatTheyWantToSearch = $_POST['field'];
尝试使用 ajax
var searchRequest = null;
$("#field").autocomplete({
maxLength: 5,
source: function(request, response) {
if (searchRequest !== null) {
searchRequest.abort();
}
searchRequest = $.ajax({
url: 'SearchTest.php',
method: 'post',
dataType: "json",
data: {term: request.term},
success: function(data) {
searchRequest = null;
response($.map(data.items, function(item) {
return {
value: item.name,
label: item.name
};
}));
}
}).fail(function() {
searchRequest = null;
});
}
});
SearchTest.php中的JSON响应示例
<?php
header('Content-type: application/json');
echo '{"items":[{"name":"Ashok"},{"name":"Rai"},{"name":"Vinod"}]}';
?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句