简体   繁体   English

选项列表上是否可以包含JQuery自动完成子文本?

[英]Is it possible to have a JQuery autocomplete sub text on the options list?

I made a fairly customized autocomplete input box, and I'd like to have the same functionality as the normal HTML select box options list where you have the gray little helper text on the side. 我做了一个相当自定义的自动完成输入框,我想拥有与普通HTML选择框选项列表相同的功能,在该列表中,您的侧面是灰色的小助手文本。 I'm picturing this: 我在想这个:

在此处输入图片说明

Is that possible? 那可能吗? Here's how I have my box set up now: 现在设置我的盒子的方法如下:

 $(input).autocomplete({ source: function(request, response){ $.get('/' + tableName + "/" + request.term, function(data){ var list = []; $.each(data, function(index, value){ list.push({'value': value.name, 'id': value.id, 'type': value.type, 'streetAddress': value.streetAddress, 'city': value.city, 'state': value.state, 'zip': value.zip }); }); response(list); }); }, ... 

Yes, this can be accomplished using the autocomplete widget's _renderItem() extension point. 是的,这可以使用自动完成窗口小部件的_renderItem()扩展点来完成。 Specifying your own custom function here allows you to override the widget's default behavior for creating and appending the autocomplete menu's <li> elements: 在此处指定您自己的自定义函数,您可以覆盖小部件的默认行为,以创建和附加自动完成菜单的<li>元素:

$(input).autocomplete({
source: function(request, response){
    $.get('/' + tableName + "/" + request.term, function(data){
      var list = [];
      $.each(data, function(index, value){
        list.push({'value': value.name, 'id': value.id, 'type': value.type, 'streetAddress':        value.streetAddress, 'city': value.city, 'state': value.state, 'zip': value.zip });
      });
    response(list);
  });
},
_renderItem: function(ul, item) {
  return $("<li>")
    .append(item.label)
    .append("<span>Your styled helper text here</span>")
    .appendTo(ul);
},
...

Of course, you will have to apply the styling and positioning of your choice to the inner <span> element in order to achieve the visual effect you are looking for. 当然,您必须将选择的样式和位置应用于内部的<span>元素,以实现所需的视觉效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM