jQuery UI 自动完成问题

发布于 2024-11-03 06:09:31 字数 901 浏览 0 评论 0原文

我正在尝试让 jQuery UI 自动完成小部件正常工作,但遇到了一些问题。
首先,我尝试从数据库中检索一些数据并将它们存储在隐藏的

标记中,格式为: item1 ;项目2; item3; 然后使用 jQuery 引用此标签并使用 .html() (结果为 null)或 .text() (结果为空)获取其内容细绳)。然后我决定在 jQuery 块内本地执行所有这些操作,并执行以下操作:

var tags = "house ; children's room ; master bedroom ... etc"
    alert(tags); //returns them in the right format
    var availableTags = tags.split(' ;');
    alert(availableTags); //returns them in the form" item1, item2, item3
    alert(availableTags[1]); //returns children's room
    $(".liTagInput").autocomplete({ 
        minLength: 2,
        source: availableTags 
    });

但这仍然无法使自动完成功能正常工作...为什么它不起作用?我确保我也添加了 CSS,所以这不应该是问题...

理论上,我仍然想从数据库中检索数据。在这种情况下,我只想将标签回显到 html 页面,然后按照开头所述进行操作。在其他情况下,结果可能太大,我想通过 AJAX 获取它们,但那是另一个故事了......我需要先让它工作,然后我会担心更复杂的场景:)

任何帮助是非常感谢!

I'm trying to get the jQuery UI autocomplete widget to work but I'm having some issues.
First, I tried to retrieve some data from the database and store them inside a hidden <p> tag in the form of: item1 ; item2; item3; and then using jQuery reference this tag and get its content using .html() (result was null) or .text() (result was empty string). Then I decided to do all of this locally inside the jQuery block and did the following:

var tags = "house ; children's room ; master bedroom ... etc"
    alert(tags); //returns them in the right format
    var availableTags = tags.split(' ;');
    alert(availableTags); //returns them in the form" item1, item2, item3
    alert(availableTags[1]); //returns children's room
    $(".liTagInput").autocomplete({ 
        minLength: 2,
        source: availableTags 
    });

But that still doesn't get the autocomplete to work... Why is it not working? I made sure I added the CSS as well, so that shouldn't be the problem...

Theoretically, I would still want to retrieve the data from the database. In this instance, I would want just echo the tags to the html page and then do as described at the beginning. In other cases where the result could be too big, I would want to get them via AJAX but that's another story... I need to get this to work first and then I'll worry about that more complex scenarios :)

Any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-11-10 06:09:31

您的演示按此处编码的方式工作: http://jsfiddle.net/aXuHq/

其他一些提示:

  • 您检查了吗您将项目附加到的隐藏元素以确保内容符合您的预期?
  • 您还可以将标签echo到JavaScript数组中(不确定您使用的是什么服务器端技术,所以我无法提供示例),然后将其直接传递给自动完成功能。
  • 使用带有自动完成功能的 AJAX 并不难设置,请查看演示

Your demo works as coded here: http://jsfiddle.net/aXuHq/

Some other tips:

  • Did you inspect the hidden element you're appending the items to to make sure the contents are what you expect?
  • You could also just echo the tags into a JavaScript array (not sure what server-side technology you're using so I won't be able to provide an example), and pass that directly to autocomplete.
  • Using AJAX with autocomplete isn't too bad to set up, check out the demo.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文