是什么导致Jquery UI autocomplete在添加_renderMenu方法时抛出TypeError?

dpiehjr4  于 2023-11-17  发布在  jQuery
关注(0)|答案(1)|浏览(131)

我得到了一个TypeError以及菜单不显示后添加_renderMenu方法.当我删除该方法,它的工作;然而,我想调整菜单的默认外观.我不知道是什么原因导致的错误,并希望任何帮助.下面是代码:

$.widget( 'ui.autocomplete', $.ui.autocomplete, {
           _renderMenu : function ( ul, items ) {
        }
    });
    var tags  =  [
       "Books",
       "Magazines",
       "Poems"
    ];
    $( "#automplete" ).autocomplete({
       source: tags
    });

字符串
这里是错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'value')
    at x.<computed>.<computed>.menufocus 
    at HTMLUListElement.i 
    at HTMLUListElement.dispatch 
    at v.handle 
    at Object.trigger
    at HTMLUListElement.<anonymous>
    at Function.each 
    at e.<computed>.each 
    at e.<computed>.trigger
    at x.<computed>.<computed>.

brccelvz

brccelvz1#

请检查这个:

$.widget('ui.autocomplete', $.ui.autocomplete, {
  _renderMenu: function(ul, items) {}
});

var tags = [
  "Books",
  "Magazines",
  "Poems"
];

$("#autocomplete").autocomplete({
  source: tags
});

个字符

完整工作示例:

$.widget('ui.autocomplete', $.ui.autocomplete, {
  _renderMenu: function(ul, items) {
    var that = this;  
    $.each(items, function(index, item) {
      that._renderItemData( ul, item );
    })
  },
  _renderItem: function(ul, item) {
  return $( "<li>" )
    .attr( "data-value", item.value )
    .append("<em>" + item.label + "</em>")
    .appendTo( ul );
  }
});

var tags = [
  "Books",
  "Magazines",
  "Poems"
];

$("#autocomplete").autocomplete({
  source: tags
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js" integrity="sha256-lSjKY0/srUM9BE3dPm+c4fBo1dky2v27Gdjm2uoZaL0=" crossorigin="anonymous"></script>

<div>
  <label for="tags">Tags: </label>
  <input id="autocomplete">
</div>

的字符串

相关问题