jquery autocomplete:this.menu undefined

问题描述 投票:3回答:2

我正在使用jQuery UI Autocomplete来处理一些数据。现在我有3个自动完成元素,其中2个工作正常,但没有。在页面的开头他给我错误elem.ownerDocument is null。当我将文本放入input字段时,他找到了结果,但我得到了错误this.menu is undefined (jquery.js line 6012),它指的是应该显示结果的ul list

这里有一些代码:

$("#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)

谢谢

javascript jquery jquery-ui jquery-autocomplete
2个回答
0
投票

我相信它是因为你的'这个'超出了范围。

在一个函数中,'this'并不总是你想象的那样。

通常的解决方案是引用你的全球'这个'

var element = $(this);

要么

var me = this;

当你在一个函数中时,请参考元素而不是'this'。

你也可以发布你的HTML吗?或指出哪一行有错误?我似乎无法从你的片段中看到它。


0
投票

您缺少自动完成引用的库。

所以,包括这个:

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

祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.