添加指向 JQueryUI 自动完成项的链接

发布于 2024-08-26 12:27:40 字数 856 浏览 3 评论 0原文

当用户开始在搜索框中输入内容时,建议页面会返回与该名称匹配的所有集合中的最新项目以及其他数据。

我想展示该项目(及其图像)以及“查看此集合中的所有项目”的链接。

我可以使用以下代码(大部分)做到这一点:

$('#search').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: suggesturl,
            dataType: 'json',
            data: request,
            success: function (data) {
                response(data.map(function (value) {
                    return {
                        'label': '<img src="' + value.thumbsmall + '" />' + value.name + '<a href="/">More items from this collection...</a>',
                        'value': value.fullname
                    };  
                }));
            }   
        }); 
    },  
    minLength: 3
})

问题是,尽管链接出现在框中,但单击它时它会被忽略,并且执行默认的 select 操作(该项目的value 被放入文本框中)。

When a user starts typing on the searchbox, the suggestion page returns the latest item from all collections matching that nama, plus other data.

I'd like to show that item (along its image), and a link to "see all items from this collection".

I can do (most of) that with the following code:

$('#search').autocomplete({
    source: function (request, response) {
        $.ajax({
            url: suggesturl,
            dataType: 'json',
            data: request,
            success: function (data) {
                response(data.map(function (value) {
                    return {
                        'label': '<img src="' + value.thumbsmall + '" />' + value.name + '<a href="/">More items from this collection...</a>',
                        'value': value.fullname
                    };  
                }));
            }   
        }); 
    },  
    minLength: 3
})

The problem is that, although the link appears in the box, when it's clicked it gets ignored, and the default select action is executed (the item's value is put into the textbox).

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

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

发布评论

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

评论(1

Bonjour°[大白 2024-09-02 12:27:40

看来你有几个选择。首先,您可以使用自动完成初始值设定项上的选择选项来指定自己的选择操作。

$(selector).autocomplete({
    source: ... ,
    select: function(value, data){
          if (typeof data == "undefined") {
              emitMessage('You selected: ' + value + "<br/>");
          } else {
              emitMessage('You selected: ' + data.item.value + "<br/>");
          }
    }
});

如果由于某种原因这还不够,那么您可以为自动完成列表呈现您自己的内容,并以这种方式获得所需的控制权。
您可以通过对 _renderItem fn 进行猴子修补来实现此目的,这是自动完成调用以呈现列表中每个项目的方法。检查此答案了解如何操作。它适用于 v1.8rc3。

我想在 _renderItem 中,您可以渲染一个可点击的 ,并将您喜欢的任何逻辑附加到单击事件。

如果您选择这种方式,您可能需要关闭默认的单击操作。我认为自动完成使用 作为提供点击事件的元素。在这种情况下,您需要取消设置该点击处理程序。

Seems like you have a couple options. First, you can specify your own select action, using a select option on the autocomplete initializer.

$(selector).autocomplete({
    source: ... ,
    select: function(value, data){
          if (typeof data == "undefined") {
              emitMessage('You selected: ' + value + "<br/>");
          } else {
              emitMessage('You selected: ' + data.item.value + "<br/>");
          }
    }
});

If that is not sufficient, for some reason, then you can render your own content for the autocomplete list, and in that way get as much control as you need.
You do that by monkey-patching the _renderItem fn, which is what autocomplete calls to render each item in the list. Check this answer for how to do it. It works in v1.8rc3.

I suppose in _renderItem you could render a clickable <span>, and attach any logic you like to the click event.

If you go that route, you may have a need to turn OFF the default click action. I think autocomplete uses an <a> for the element that provides the click event. In that case, you'll need to unset that click handler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文