大家好我已经看到了与此问题相关的所有问题,但无法找到解决方案。我正在使用jquery ui autocomplete。我的情况就是这样,
我有一个复选框。选中它时,会向服务器发送ajax请求,该服务器从数据库中获取一些信息并返回json编码数据。在成功函数上,我调用jquery自动完成,在源代码中我提供了我的ajax请求返回的数据。当我开始在我的文本框中写一些东西时,(我可以在firebug控制台选项卡中看到)发送get
请求,其中包含从ajax请求返回的所有选项,如此
http://localhost/project/module/controller/index/%5B%22Mobile%22,%22Watch%22,%22Lamps%22,%22Harry%20Potter%22,%22Suitcase%22,%22Halloween%22,%22Xmas%22,%22Multiple%22,%22oil%22,%22Empty%20Keywords%22,%22iphone%20theme%22,%22Edit%20theme%22,%22Final%20Theme%22%5D?term=mo
没有任何反应。没有选项显示为自动完成。我的代码如下
<input type="checkbox" name="prebuilt-Themes" id="prebuilt-Themes" onclick="get_all_themes();">
文字框
<input type="text" id="themes" style="display: none;" class="ui-autocomplete-input">
处理ajax请求的函数
function get_all_themes()
{
if($("#prebuilt-Themes").is(':checked'))
{
$('#themes').show();
$.ajax({
type:'POST',
//data:({string:'abc'}),
url: "<?= $this->baseUrl(); ?>/module/controller/getallthemes",
success:function(data)
{
$(function() {
$( "#themes" ).autocomplete({
source: data
});
});
}
});
}
}
我从Ajax请求收到的数据如下所示
["Mobile","Watch","Lamps","Harry Potter","Suitcase","Halloween","Xmas","Multiple","oil","Empty Keywords","iphone theme","Edit theme","Final Theme"]
任何人都可以指导我做错了吗?
答案 0 :(得分:1)
您需要使用id,label和value来生成json格式的ajax响应,如下所示:
[ { "id": "Luscinia svecica", "label": "Bluethroat", "value": "Bluethroat" }, { "id": "Motacilla flava", "label": "Blue-headed Wagtail", "value": "Blue-headed Wagtail" }, { "id": "Cyanistes caeruleus", "label": "Blue Tit", "value": "Blue Tit" }, { "id": "Monticola solitarius", "label": "Blue Rock Thrush", "value": "Blue Rock Thrush" }, { "id": "Anas discors", "label": "Blue-winged Teal", "value": "Blue-winged Teal" }, { "id": "Merops persicus", "label": "Blue-cheeked Bee-eater", "value": "Blue-cheeked Bee-eater" }, { "id": "Tarsiger cyanurus", "label": "Red-flanked Bluetail", "value": "Red-flanked Bluetail" } ]