Showing no results message on jquery autocomplete

bgc*_*ode 9 jquery autocomplete

I'm using the jQuery autocomplete plugin, NOT the UI autocomplete. I would like to make an unclickable No Results message appear whenever they enter something that has no results from the autocomplete. How can I do that?

Sha*_*ood 5

这是一个需要对jquery.autocomplete.js进行一些小编辑的解决方案.

在jquery.autocomplete.js中:

将函数hideResultsNow()编辑为它调用emptyData()的第一件事

function hideResultsNow() {

    $('#emptyData').show();
    ...
}
Run Code Online (Sandbox Code Playgroud)

更改输入字段上的按键绑定以在每次按键后隐藏#emptyData

$input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {

    $('#emptyData').hide();
    ...
}
Run Code Online (Sandbox Code Playgroud)

选择成功后隐藏#emptyData(在它返回true之前)

function selectCurrent() {

    ...
    $('#emptyData').hide();
    return true;
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要在输入框下方的HTML中添加一个名为#emptyData的相同样式列表的div.这是我使用的测试页面的完整HTML.

<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/demo/main.css" type="text/css" />
  <link rel="stylesheet" href="http://dev.jquery.com/view/trunk/plugins/autocomplete/jquery.autocomplete.css" type="text/css" />

  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.bgiframe.min.js"></script>
  <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/autocomplete/lib/jquery.dimensions.js"></script>
  <script type="text/javascript" src="jquery.autocomplete.js"></script>
  <script>
  $(document).ready(function(){
    var data = "Core Selectors Attributes Traversing Manipulation CSS Events Effects Ajax Utilities".split(" ");
  $("#example").autocomplete(data);

  });
  </script>

</head>
<body>
  API Reference: <input id="example" /> (try "C" or "E")


  <div id='emptyData' class='ac_results' style='display: none; position: absolute; width: 151px; top: 20px; left: 91px;'>
    <ul style="max-height: 180px; overflow: auto;">
      <li id='noDataMsg' >No Results</li>
    </ul>
  </div>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

#emptyData的位置取自自动完成位置,CSS样式与正确答案不完全相同,但除此之外,我认为不应该有任何需要更改的内容.希望能解决你的问题.