我在我的应用程序中使用jquery自动完成功能,但我对如何处理单击自动完成项目感到不满意:
$(function () {
$("input#autocomplete").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("Search", "Navigation")', type: "POST", dataType: "json",
data: { searchTerm: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Name,
value: item.CityId
}
}))
}
})
},
select: function (event, ui) {
window.location.href = '@Url.Action("Index", "Home")' + '/' + ui.item.value;
}
});
});
这种方法有效,但有时不起作用
有没有办法将@Html.ActionLink...
添加为项目?
答案 0 :(得分:4)
试试这个
function onItemSelect(item){
var siteRootUrl = '@Url.Content("~/")';
window.location.href = siteRootUrl + 'controller/action/' + item;
}
$(function () {
$("input#autocomplete").autocomplete({
source: function (request, response) {
$.ajax({
url: '@Url.Action("Search", "Navigation")', type: "POST", dataType: "json",
data: { searchTerm: request.term, maxResults: 10 },
success: function (data) {
response($.map(data, function (item) {
return { label: item.Name,
value: item.CityId
}
}))
}
})
},
select: function (event, ui) {
onItemSelect(ui.item.value);
}
});
});