jQuery自动完成类别位置

时间:2013-02-28 19:48:39

标签: jquery jquery-ui autocomplete

我用这个:

$( "#glavnotursene" ).autocomplete({
    delay: 0,
    source: 'extrastuff.php?glavnotursene',
    select: function( event, ui ) {
        window.location = 'maps.php?id. ' + ui.item.nomer;
    }
});

它完美无缺。当项目弹出并点击它们时,我被重定向到maps.php 我决定使用类别,所以我这样做:

    $.widget( "custom.catcomplete", $.ui.autocomplete, {
            _renderMenu: function( ul, items ) {
                var self = this,
                    currentCategory = "";
                $.each( items, function( index, item ) {
                    if ( item.category != currentCategory ) {
                        ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                        currentCategory = item.category;
                    }
                    self._renderItem( ul, item );
                });
            }
        });

$( "#glavnotursene" ).catcomplete({
        delay: 0,
        source: 'extrastuff.php?glavnotursene',
        select: function( event, ui ) {
            window.location = 'maps.php?id. ' + ui.item.nomer;
        }
    });

并且选择将不再起作用,因此人们不会被重定向。

我正在使用:

  • jQuery UI - v1.10.0

  • jQuery v1.9.1

1 个答案:

答案 0 :(得分:1)

尝试以这种方式:

$( "#glavnotursene" ).autocomplete({
   delay: 0,
   source: 'extrastuff.php?glavnotursene',
   select: function( event, ui ) {
       window.location = 'maps.php?id. ' + ui.item.nomer;
   }
}).data("autocomplete")._renderMenu = function(ul, items) {
  var self = this, currentCategory = "";
  $.each( items, function( index, item ) {
     if ( item.category != currentCategory ) {
        ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
        currentCategory = item.category;
     }
     self._renderItem( ul, item );
  });
};

如果有效,则仅适用于$('#glavnotursene')。使其成为通用的一些方法是将其分配给外部函数:

function my_autocomplete(ul, items, self){
  var currentCategory = "";
  $.each( items, function( index, item ) {
     if ( item.category != currentCategory ) {
        ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
        currentCategory = item.category;
     }
     self._renderItem( ul, item );
  });
}

$( "#glavnotursene" ).catcomplete({
   delay: 0,
   source: 'extrastuff.php?glavnotursene',
   select: function( event, ui ) {
       window.location = 'maps.php?id. ' + ui.item.nomer;
   }
}).data("autocomplete")._renderMenu = function(ul, items) {
  my_autocomplete(ul, items, this);
};

我没有检查这是否有效,但我希望它会。