【发布时间】: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