jQuery ui autocomplete - renderItem

spe*_*ked 5 javascript jquery jquery-ui autocomplete

我正在使用_renderItem来修改结果列表

.data( "autocomplete" )._renderItem = function( ul, item ) {
            var temp = item.url.substring(16, item.url.length)
            return $( "<li></li>" )
            .data( "item.autocomplete", item )
            .append( "<a>" + item.value + "<br>" + item.url + "<br>" + item.description + "<br>" + "Support URL: " + item.support_url + "<br>" + "Contact: " + "<a href=" + item.contact + ">Test</a>" + "<br />" + "</a>"  )
            .appendTo( ul )
Run Code Online (Sandbox Code Playgroud)

这具有自动标记任何看起来像url作为href的行为的行为.我想将整个项目作为链接

在较旧的自动完成中,这样做:

 .result(function(event, item) {
   location.href = item.url;
  });
Run Code Online (Sandbox Code Playgroud)

但是这不再需要支持了.

有谁知道我怎么做:

1)使用类似于.result函数的东西,只需将整个项目作为链接

2)修改_renderItem,使其不会自动将看起来像URL的字符串转换为href的

谢谢.

小智 10

看来,这在以前的版本中已经发生了变化.现在你必须使用

$element.data('uiAutocomplete')._renderItem()
Run Code Online (Sandbox Code Playgroud)


Oli*_*oyd 5

自定义 jQuery 自动完成的更好方法是使用 widgets 创建您自己的扩展版本

$.widget( "custom.mySpecialAutocomplete", $.ui.autocomplete, {
  // Add the item's value as a data attribute on the <li>.
  _renderItem: function( ul, item ) {
    return $( "<li>" )
      .attr( "data-value", item.value )
      .append( $( "<a>" ).text( item.label ) )
      .appendTo( ul );
  },
  // Add a CSS class name to the odd menu items.
  _renderMenu: function( ul, items ) {
    var that = this;
    $.each( items, function( index, item ) {
      that._renderItemData( ul, item );
    });
    $( ul ).find( "li:odd" ).addClass( "odd" );
  }
});

var availableTags = [
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"
];

$('#myElement').mySpecialAutocomplete({
  source: availableTags
});
Run Code Online (Sandbox Code Playgroud)


小智 3

当您定义自动完成功能时,请使用 select 函数创建链接:

$('selector').autocomplete({
    source: ...,
    select: function(event, ui) { window.location = ui.url; }
});
Run Code Online (Sandbox Code Playgroud)