2011-05-05 126 views
3

我對某些數據使用jQuery UI自動完成功能。現在我有3個自動完成元素,其中2個工作正常,一個沒有。在頁面開始處,他給了我錯誤elem.ownerDocument is null。當我將文本放入input字段中時,他找到了結果,但我得到了錯誤this.menu is undefined (jquery.js line 6012),它指的是ul list,其中應顯示結果。jquery自動完成:this.menu undefined

這裏是一些代碼:

$("#iName").autocomplete({ 
    source: widget.festivals_list, 
    autofocus: true, 
    focus: function (e, ui) { 
     return false; 
    }, 
    search: function (event, ui){ 
     ownFest = true; 
     $("#iDate").removeAttr("disabled"); 
     $("#iTime").removeAttr("disabled"); 
    }, 
    select: function (event, ui) { 
     ownFest = false; 
     $(event.target).val(ui.item.label); 
     selectedN = ui.item.value; 
     $(widget.festivals).each(function fn(){ 
      if(this.id == ui.item.value){ 
       $("#iDate").val(this.date).attr("disabled", "disabled"); 
       $("#iTime").val(this.time).attr("disabled", "disabled"); 
      } 
     }); 
     return false; 
    } 
}); 

HTML代碼:

<table> 
        <tr> 
         <td>Type the name</td> 
        </tr> 
        <tr> 
         <td><input type="text" id="iFest"/></td> 
        </tr> 
       </table> 

這造成我的input標記屬性的典型數量並創建ul列表。

<ul class="ui-autocomplete ui-menu ui-widget ui-widget-content ui-corner-all" role="listbox" aria-activedescendant="ui-active-menuitem"></ul> 

<input id="iFest" class="ui-autocomplete-input" type="text" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true"> 

有人也有這些問題嗎? 感謝

(使用jQuery 1.5.2和jQuery UI 1.8.11)

感謝

回答

0

我相信它,因爲你的 '這' 超出範圍。

在函數內部,'this'並不總是您認爲的那樣。

通常的解決辦法是做一個參考,以全局「這」

var element = $(this); 

var me = this; 

,並參考要素,而不是「本」,當你在一個函數是。

你可以發佈你的HTML嗎?或指出哪一行有錯誤?我似乎無法從您的摘錄中看到它。

+0

更改了我的問題的內容 – jeroenjoosen 2011-05-05 13:22:26

0

您缺少自動完成引用的庫。

因此,包括此:

<script type="text/javascript" src="js/ui/jquery.ui.menu.js"></script> 

祝你好運!