过滤jQuery自动完成中的响应 [英] Filter response in jQuery autocomplete

查看:78
本文介绍了过滤jQuery自动完成中的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现jQuery.autocomplete,我用它来输入标签(类似于SO上的此处).但是,我不想显示列表中已经存在的标签.如果我已经添加了foo,我不希望它显示为建议.标签列表是从我的服务器中获取的.但是,我在更改ui.content对象时遇到一些问题.

I'm trying to implement jQuery.autocomplete which I use to enter tags (much like here on SO). However, I don't want to show tags that I have already in my list. If I have added foo already I don't want it to show up as a suggestion. The list of tags are fetched from my server. However, I have some issues with altering the ui.content object.

我使用在此处找到的响应方法:

I use the response method found here:

response: (event, ui)->
  # Fetch already added tags
  current = $('input#photo_tag_list').val().split(',')
  # Filter out already added tags from result
  ui.content = ui.content.filter (el) -> $.inArray(el.label, current) == -1

但是,以上操作无效.它仍然显示所有标签.如果我检查ui.content,我可以看到它看起来正确...以上是异步操作还是什么?!

However, the above doesn't work. It still shows all tags. If I inspect ui.content I can see that it looks correct... Is the above async or something?!

奇怪的是,如果我这样做:

The weird thing is that if I do:

ui.content.push({label: "Foo", value:"Bar"})

Foo出现在自动完成列表中.但是,如果我这样做:

Foo shows up in the autocomplete list. But if I do this:

ui.content = []

它仍然显示返回的所有内容.我希望有一个空列表.

It still shows everything returned. I would expect an empty list.

为什么会这样?如何正确修改结果列表?

Why would this be? How do I modify the result list correctly?

更新 根据安德鲁·惠特克(Andrew Whitaker)的解决方案,使它可以正常工作.

Update Got it to work based on Andrew Whitaker's solution.

推荐答案

好,我是根据安德鲁·惠特克(Andrew Whitaker)的回答得到的.

Ok, I got it based on Andrew Whitaker's answer.

文字:

response: (event, ui)->
  current = $('input#photo_tag_list').val().split(',')
  for el, i in ui.content by -1
    if $.inArray(el.label, current) != -1
      ui.content.splice(i, 1)

太棒了!

这篇关于过滤jQuery自动完成中的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆