jquery自动完成渲染问题
我试图通过 servlet 接收数据来实现 jQuery 自动完成插件。 该脚本有效,并且确实从 servlet 中提取结果,但我无法让它在下拉菜单中正确呈现。假设我在搜索栏中写“hel”,自动完成下拉列表是:
h
e
l
l
o
根据我的理解,自动完成插件应该接收一个数组,我认为这可能是问题所在。 这是我的 javascript 代码:
<script>
function getFilteredNames(request, response){
$.ajax({
type: "POST",
url: "../servlet/Autocomplete",
data: request,
success: function(data) {
response(data);
}
});
}
$(document).ready(function(){
$("#buscar").autocomplete({ source : getFilteredNames, minLength: 3});
});
</script>
这是 servlet 代码的一部分:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String query = request.getParameter("term");
List<Socio> rs = new ArrayList<Socio>();
rs = SocioDAO.getSociosByQuery(query);
if (rs.isEmpty()) {
out.println("No hay coincidencias");
}
else {
Iterator <Socio> iterator = rs.iterator();
while (iterator.hasNext()) {
String nombre = (String) iterator.next().getNombre();
out.println(nombre);
}
}
任何建议将不胜感激! 谢谢, 缺口
I am trying to implement the jQuery autocomplete plugin by receiving the data through a servlet.
The script works and it does pull the results from the servlet but I can't get it to render properly in the drop down menu. Say I write "hel" in the search bar, the autocomplete drop down list is:
h
e
l
l
o
From what I understand, the autocomplete plugin is supposed to receive an array and I think that may be the issue.
This is my javascript code:
<script>
function getFilteredNames(request, response){
$.ajax({
type: "POST",
url: "../servlet/Autocomplete",
data: request,
success: function(data) {
response(data);
}
});
}
$(document).ready(function(){
$("#buscar").autocomplete({ source : getFilteredNames, minLength: 3});
});
</script>
And this is part of the servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
String query = request.getParameter("term");
List<Socio> rs = new ArrayList<Socio>();
rs = SocioDAO.getSociosByQuery(query);
if (rs.isEmpty()) {
out.println("No hay coincidencias");
}
else {
Iterator <Socio> iterator = rs.iterator();
while (iterator.hasNext()) {
String nombre = (String) iterator.next().getNombre();
out.println(nombre);
}
}
Any suggestions would be greatly appreciated!
Thanks,
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动完成 UI 插件接受 JSON 数据格式的数组、对象。
通过ajax调用发送到客户端。
为了将你的数据转换为 json 格式,你可以使用 http://www.json.org/ 提供的实用程序调用
The autocomplete UI plugin accepts array,object formatted in JSON data.
sending to client side through ajax call.
for converting ur data to json format u can use utility call provided by http://www.json.org/