我已经花费了几个小时来研究这段代码片段,但是我无法理解jQuery的自动完成UI是如何工作的。我按照这个教程 http://viralpatel.net/blogs/tutorial-create-autocomplete-feature-with-java-jsp-jquery/ 的步骤操作。 我使用了相同的示例,但是我使用了一个servlet代替JSP发送请求。请求到达了名为“Fetcher”的servlet,它也被执行了,但是页面上没有返回任何内容。以下是代码。
public class Fetcher extends HttpServlet {
[...]
List<String> countryList = new ArrayList<String>();
String param = request.getParameter("term");
countryList.add("USA");
countryList.add("Pakistan");
countryList.add("Britain");
countryList.add("India");
countryList.add("Italy");
countryList.add("Ireland");
countryList.add("Bangladesh");
countryList.add("Brazil");
countryList.add("United Arab Emirates");
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
response.setHeader("Cache-Control", "no-cache");
for(String country : countryList){
out.println(country);
}
[...]
}
HTML中的Javascript片段:
<script>
$(function() {
$( "#tags" ).autocomplete({
source: "Fetcher"
});
});
</script>
HTML表单:
<label for="tags">Tags: </label>
<input id="tags" />
这个页面的示例似乎是为熟练掌握jquery的人编写的,http://jqueryui.com/autocomplete/#default。请问有人能详细解释一下它是如何工作的,这样我就可以在其他地方使用它了。
$("#tags").autocomplete("url")
,但你正在使用$("#tags").autocomplete({source:"url"})
。如果你有特别原因需要不同于教程的方式进行,请问为什么现在抱怨它不能正常工作呢?你应该使用$("#tags").autocomplete("Fetcher")
。 - BalusC