【问题标题】:autocomplete is not working in ASP.NET MVC partial view自动完成在 ASP.NET MVC 部分视图中不起作用
【发布时间】:2016-03-13 11:26:24
【问题描述】:

我对文本框有部分视图

<input type="text" name="models" id="models" />

在我的 _Layout.csthml 脚本部分中,我有脚本

$('#models').on('input propertychange paste', function () { 
    var target = $(this);
    target.autocomplete({
        source: function (request, response) {
            $.post(@Url.Action("MyAction", "MyController"), request, response);
        },
        minLength: 1,
        delay: 1000
    });
});

但是,当我在输入中输入一些文本时,没有任何反应,也没有出现错误。我怀疑原因是加载 DOM 时 #models 还不存在。我正在考虑使用全局 ajaxSuccess 函数,但我不确定这是否正确。当我在输入中输入文本并递归时也会调用它吗?这里的最佳做法是什么?

我尝试像这样使用 ajaxSuccess:

$(document).ajaxSuccess(function() {
        $("#models").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/MyController/MyAction",
                    type: "POST",
                    dataType: "json",
                    data: { searchText: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Text, value: item.Text };
                        }));

                    }
                });
            },
        });
    });

但出现错误

未捕获的类型错误:$(...).autocomplete 不是函数

我确信 jQuery 在页面上和我的脚本之前只加载一次。 Intellisense 根本看不到自动完成功能。

【问题讨论】:

标签: jquery asp.net-mvc autocomplete partial-views


【解决方案1】:

对不起,我真的很愚蠢,没有意识到自动完成不是主 jquery 包的一部分,但它在 jquery UI 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 2021-02-07
    • 2017-05-29
    • 2012-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多