Making a autocomplete textbox/input area

2019-09-20 22:44发布

问题:

I am trying to make a jQuery autocomplete inputbox, so that people can see suggestions that are pre-set. Not that hard actually, but somehow, I can't seem to solve a problem which won't show up the suggestions.

Here is a fiddle: http://jsfiddle.net/tbBy6/

And this is my HTML file:

<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
  <script>
  $(document).ready(function() {
    $("input#search-textbox").autocomplete({
    source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]
});
  });
  </script>
</head>

Textbox:

<body>
<input id="search-textbox" class="topic-picker ui-autocomplete-input" type="text" maxlength="100" name="q" accesskey="b"  autocomplete="off" placeholder="enter text" role="textbox" aria-autocomplete="list" aria-haspopup="true">

Why can't I see a suggestion if I type "java" for example?

And my second question: How can I get data from the database to be fetched?

PS: I used the code from here: http://docs.jquery.com/UI/API/1.8/Autocomplete

回答1:

If you're testing in the fiddle you need to change the framework on the left to jQuery, not MooTools, and you will also need to include jQuery UI.

Try this one:

Updated fiddle