2011-05-05 128 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> 

祝你好运!