duanba3707
2016-07-24 11:03
浏览 81

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

I have implemented JQuery Ui autocomplete function to display content from the database using the below code

Script

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

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 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 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

enter image description here

And here is the result that I'm getting in chrome ->Network

[{name: "asa"}, {name: "Abhijit Das"}, {name: "Abhijit Das"}, {name: "Abhijit Das"}]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douya8978 2016-07-24 11:31
    已采纳

    As @marmeladze suggested, your problem is most likely the response format of the php code. According to https://jqueryui.com/autocomplete/ you should have a simple array like this:

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

    According to http://api.jqueryui.com/autocomplete/ Multiple types are supported:

    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. 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, e.g., 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):

    [
      {"id":"Nycticorax nycticorax",
       "label":"Black-crowned Night Heron",
       "value":"Black-crowned Night Heron"},
      {"id":"Corvus cornix",
       "label":"Hooded Crow",
       "value":"Hooded Crow"}
    ]
    
    点赞 打赏 评论

相关推荐 更多相似问题