简体   繁体   English

jQuery自动完成-在自定义div中显示结果

[英]jquery autocomplete - display results in a custom div

I'm looking for some help in customizing the dropdown that displays the results of autocomplete. 我在自定义显示自动完成结果的下拉列表中寻求帮助。 I have the following html which I want to use to display the results. 我有以下要用于显示结果的html。 The idea is that the div below is hidden to begin with and as soon there is a match, I make the div visible and each matched results would be wrapped in the < li > tag below. 这个想法是,下面的div首先是隐藏的,一旦有了匹配项,我就使div可见,并且每个匹配的结果都将包裹在下面的<li>标记中。

<div class="search_dropdown_wrapper" style="display:none;">
    <div id="search_arrow" class="dropdown_pointer search"></div>
    <ul>
        <li>
            <img src="/assets/2c42cdf.jpg"  />
            <h4>Tom Jerry</h4>
            <p>Cartoon Characters</p>
        </li>
        <li>
            ...
        </li>
        ...  
    </ul>
</div>

The portion of my autocomplete code which works to display the results is.. 我的自动完成代码中用于显示结果的部分是..

  $('#search_name').autocomplete({
    minLength: 2,
    ...
    ...
  })
  .data( "autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li></li>" )
      .data( "item.autocomplete", item )
      .append( "<a>" + item.name + "</a>" )
      .appendTo( ul );
  };

(FYI, the above thanks to How to set-up jquery-ui autocomplete in Rails ) (仅供参考,这要归功于如何在Rails中设置jquery-ui自动完成功能

At the moment I only have the name (item.name) displaying in the dropdown. 目前,下拉列表中仅显示名称(item.name)。 How can I get the html I want into the code above. 我如何才能将我想要的html插入上面的代码中。 The part where I'm confused is how to get the div around the ul item which is being passed into the function. 我感到困惑的部分是如何获取被传递给函数的ul项周围的div。 Thanks. 谢谢。

Use .parent() to get to the div around the ul: http://api.jquery.com/prev/ 使用.parent()进入ul附近的div: http ://api.jquery.com/prev/

$(ul).parent()

Or, to grab the previous sibling: (as I somehow though you needed to do at first) http://api.jquery.com/prev/ 或者,抢先一个兄弟:(尽管我首先需要做的是我想做的) http://api.jquery.com/prev/

$(ul).prev()

If ul is already a jQuery object, then you can just do ul.parent() and ul.prev() 如果ul已经是jQuery对象,则只需执行ul.parent()和ul.prev()

Alternatively, you can access the wrapper div directly: 或者,您可以直接访问wrapper div:

$('.search_dropdown_wrapper')

I might suggest adding an ID to that wrapper and accessing it by ID, eg: 我可能建议向该包装添加一个ID并按ID访问它,例如:

<div class="search_dropdown_wrapper" id="search_dropdown_wrapper">
...
</div>

Then in your script you can use $('#search_dropdown_wrapper') to access the div directly. 然后,您可以在脚本中使用$('#search_dropdown_wrapper')直接访问div。

The direct access approach is safer. 直接访问方法更安全。 If you ever change your markup, it will still access the correct div (theoretically.) 如果您更改了标记,它仍将访问正确的div(从理论上来说)。

Each item it is response objects collection. 每个项目都是响应对象集合。 If in your reply the object has a field of the email access will be item.email. 如果在您的答复中对象具有电子邮件访问权限的字段将为item.email。 It is insert your autocomplete object into custom div with custom li tag: 它是使用自定义li标签将您的自动填充对象插入自定义div中:

$("#search_name").autocomplete({
  source: availableTags,
  open: function() {
    $(this).autocomplete("widget")
      .appendTo(".search_dropdown_wrapper")
      .css("position", "static");
    } 
}).data('ui-autocomplete')._renderItem = function(ul, item) {
   var html, paragraph;
   html = '<h4><a>' + item.name + '</a></h4>';
   paragraph = '<p>Cartoon Characters</p>';
   html = html + paragraph;
   return $('<li>').append(html).appendTo(ul);
};

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

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