简体   繁体   English

使用Ajax PHP MYSQL的jQuery UI自动完成不显示结果

[英]JQuery UI autocomplete with Ajax PHP MYSQL not displaying result

I have implemented JQuery Ui autocomplete function to display content from the database using the below code 我已经实现了JQuery Ui autocomplete功能,以使用以下代码显示database中的内容

Script 脚本

<script>
$(function() {
$( "#query" ).autocomplete({
  source: 'search.php'
});
});
</script>

HTML 的HTML

<div class="col-md-9 col-sm-8 col-xs-8 " >
  <input style="width:100%;"class="form-control"  id="query" placeholder="Search" type="text">       
</div>

When I run the above I get the following result within span notification 当我运行上面的命令时,在span notification得到以下结果

<span role="status" aria-live="assertive" aria-relevant="additions" class="ui-helper-hidden-accessible">
<div style="display: none;">
 3 results are available, use up and down arrow keys to navigate.</div>
<div style="display: none;">
4 results are available, use up and down arrow keys to navigate.</div>
<div>4 results are available, use up and down arrow keys to navigate.</div></span>

But the autocomplete UL>li filed does not hold any value 但是自动完成的UL>li字段没有任何值

<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content" id="ui-id-1" tabindex="0" style="display: none; top: 902.452px; left: 72.7257px; width: 372px;">
<li class="ui-widget-content ui-menu-divider"></li>
<li class="ui-widget-content ui-menu-divider"></li>
<li class="ui-widget-content ui-menu-divider"></li>
<li class="ui-widget-content ui-menu-divider"></li>
</ul>

This is what I'm able to see 这就是我所能看到的

在此处输入图片说明

And here is the result that I'm getting in chrome ->Network 这是我进入chrome ->Network

[{name: "asa"}, {name: "Abhijit Das"}, {name: "Abhijit Das"}, {name: "Abhijit Das"}]

As @marmeladze suggested, your problem is most likely the response format of the php code. 正如@marmeladze所建议的,您的问题很可能是php代码的响应格式。 According to https://jqueryui.com/autocomplete/ you should have a simple array like this: 根据https://jqueryui.com/autocomplete/,您应该有一个简单的数组,如下所示:

[
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++"
];

According to http://api.jqueryui.com/autocomplete/ Multiple types are supported: 根据http://api.jqueryui.com/autocomplete/支持多种类型:

Array: An array can be used for local data. 数组:数组可用于本地数据。 There are two supported formats: An array of strings: [ "Choice1", "Choice2" ] An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ] The label property is displayed in the suggestion menu. 支持两种格式:字符串数组:[“ Choice1”,“ Choice2”]具有标签和值属性的对象数组:[{标签:“ Choice1”,值:“ value1”},...]标签属性显示在建议菜单中。 The value will be inserted into the input element when a user selects an item. 当用户选择一个项目时,该值将插入到输入元素中。 If just one property is specified, it will be used for both, eg, if you provide only value properties, the value will also be used as the label. 如果仅指定一个属性,则将同时使用这两个属性,例如,如果仅提供值属性,则该值也将用作标签。

Another example with remote server ( https://jqueryui.com/autocomplete/#remote ): 远程服务器的另一个示例( https://jqueryui.com/autocomplete/#remote ):

[
  {"id":"Nycticorax nycticorax",
   "label":"Black-crowned Night Heron",
   "value":"Black-crowned Night Heron"},
  {"id":"Corvus cornix",
   "label":"Hooded Crow",
   "value":"Hooded Crow"}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM