jQuery UI AutoComplete: Only allow selected valued from suggested list

前端 未结 9 1963
误落风尘
误落风尘 2020-11-30 22:48

I am implementing jQuery UI Autocomplete and am wondering if there is any way to only allow a selection from the suggested results that are

9条回答
  •  盖世英雄少女心
    2020-11-30 23:36

    I got the same problem with selected not being defined. Got a work-around for it and added the toLowerCase function, just to be safe.

    $('#' + specificInput).autocomplete({ 
      create: function () {
        $(this).data('ui-autocomplete')._renderItem = function (ul, item) {
          $(ul).addClass('for_' + specificInput); //usefull for multiple autocomplete fields
          return $('
  • ' + item.value + '
  • ').appendTo(ul); }; }, change: function( event, ui ){ var selfInput = $(this); //stores the input field if ( !ui.item ) { var writtenItem = new RegExp("^" + $.ui.autocomplete.escapeRegex($(this).val().toLowerCase()) + "$", "i"), valid = false; $('ul.for_' + specificInput).children("li").each(function() { if($(this).text().toLowerCase().match(writtenItem)) { this.selected = valid = true; selfInput.val($(this).text()); // shows the item's name from the autocomplete selfInput.next('span').text('(Existing)'); selfInput.data('id', $(this).data('id')); return false; } }); if (!valid) { selfInput.next('span').text('(New)'); selfInput.data('id', -1); } } }

提交回复
热议问题