gpt4 book ai didi

java - JQuery-UI 自动完成下拉菜单未显示所需结果

转载 作者:行者123 更新时间:2023-12-01 10:45:42 26 4
gpt4 key购买 nike

我使用 jquery-ui 实​​现了一个自动完成文本框。但它只是出现微小的行,而不是显示所需的结果。它是这样的:

enter image description here

index.jsp:

<body>
<h1>Hello Auto complete</h1>
<br> <br> <br>

<input id="search">
<input type="hidden" id="search-id">
</body>

filter.js

$(document).ready(function () {
$(function () {
$("#search").autocomplete({
source: function (request, response) {
$.ajax({
url: "SearchDB",
type: "GET",
data: {
term: request.term
},
dataType: "json",
success: function (data) {
response(data);
// console.log(data);
}

});
}
});

});
});

SearchDB servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
String term = request.getParameter("term");
Session session = com.search.connection.HibernateUtil.getSessionFactory().openSession();
Criteria criteria = session.createCriteria(Product.class);
criteria.add(Restrictions.like("name", term + "%"));
List<Product> proList = criteria.list();
List<Product> myList = new ArrayList<>();
for (Product product : proList) {
myList.add(product);
}
Gson gson = new Gson();
String result = gson.toJson(myList);
response.getWriter().write(result);

}

我的 json 响应:(当我在文本框中键入“c”时)

[{"id":5,"name":"COD 4","qnty":12,"dateAdded":"Dec 12, 2015"},{"id":6,"name":"COD 5","qnty":20,"dateAdded":"Oct 28, 2015"},{"id":7,"name":"Cars ","qnty":13,"dateAdded":"Dec 30, 2014"}]

控制台输出对我来说看起来也不错:

enter image description here

提前致谢。

最佳答案

您需要在 json 中添加“value”,我将“name”替换为“value”:

[{"id":5,"value":"COD 4","qnty":12,"dateAdded":"Dec 12, 2015"},{"id":6,"value":"COD 5","qnty":20,"dateAdded":"Oct 28, 2015"},{"id":7,"value":"Cars ","qnty":13,"dateAdded":"Dec 30, 2014"}]

关于java - JQuery-UI 自动完成下拉菜单未显示所需结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34192596/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com