2012-11-29 33 views
5

我的頁面上有一個自動完成功能,可以正確提取並顯示數據....當它停止正常工作時,就是選擇事件....jQuery用戶界面 - >自動完成 - > ON選擇 - >用戶界面項目未定義

$("#fld_search1").catcomplete({ 
     delay: 50, 
     minLength: 2, 
     open: function(e, ui){ 
      if(searching) return; 
      //console.log($(this).data('catcomplete')); 
      var acData = $(this).data('catcomplete'); 
      var styledTerm = '<strong>%s</strong>'.replace('%s', acData.term); 


      acData.menu 
       .element 
       .find('li a') 
       .each(function() { 
        var me = $(this); 
        me.html(me.text().replace(acData.term, styledTerm)); 
       }); 
      //return match.replace(new RegExp("("+keywords+")", "gi"),'<i>$1</i>'); 
     }, 
     select: function(event, ui) { 
      var I = ui.item; 
      top.console.log(ui); 
      $("#fld_search1").catcomplete("close"); 

      $('#fld_search1').val(I.name); 
      window.location = '/podjetje/'+I.value+'.html'; 
      //$('#frm_company_id').val(I.value); 
      return false; 
     }, 
     source: function(request, response) { 


      search_term = request.term; 

      if (search_term in cache) { 
       response(cache[ tesearch_termrm ]); 
       return; 
      } 


      var suggest_url = "/companies/find_company.json"; 


      $.ajax({ 
       url: suggest_url, 
       dataType: "json", 
       type : "POST", 
       data: { 
        owner: request.term 
       }, 
       success: function(data) { 
        response($.map(data, function(item) { 
         var alabel = item.label.replace(
           new RegExp('(' + 
            $.ui.autocomplete.escapeRegex(request.term) + 
            ')'), 
          "<b>$1</b>"); 
         return { 
          value: item.value, 
          label: item.label, 
          name: item.name, 
          category: item.category 
         } 
        })); 
       } 
      }); 


     } 
    }); 

所以,如果我做top.console.log(UI),我得到一個對象有一個財產而它沒有得到UI對象...

>項......這是不確定的。 ..所以如果我記錄我的價值,我得到未定義...這可能是怎麼回事?

這是1.9.1

,如果我改變它,使用1.9.2菜單關閉ALWAYS鼠標懸停......如果我使用自動對焦功能,它甚至不開!

回答

27

今天我遇到了與undefined ui.item屬性相同的問題。經過一些調試,我找到了解決方案。 jQuery UI團隊稍微改變了Autocomplete with categories示例代碼。當您查看示例源代碼,你會看到:

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

that._renderItemData(ul, item);線曾經是(預1.9版本)that._renderItem(ul, item);。另見:bug #8560。但這在1.9 Upgrade Guide中沒有提及。

所以我改變我的插件使用_renderItemData(...,...)函數,並解決了這個問題。

希望這會有所幫助!

+1

爲你+1,你的答案節省我的時間。我剛剛從1.8.12將JQuery UI升級到1.10.3,並且選擇函數在'ui.item'中獲取'undefined'。在將'that._renderItem(ul,item)'改爲'that._renderItemData(ul,item)'後,它再次工作。 –

+0

哇感謝的人,你今天救了我:) – 360Airwalk

相關問題