我在asp.net MVC Web应用程序中工作,我正在使用jQuery版本1.8.2& jQuery-ui 1.8.24。我有以下自动完成代码: -
$("#ServerTag,#SDTag,#FirewallTag,#RouterTag,#SwitchTag").live("focus.autocomplete", null, function () {
var URL ="@Url.Content("~/Server/AutoComplete")";
$(this).autocomplete({
minLength: 1, delay: 1000,
source: function (request, response) {
$.ajax({
url: URL,
cacheLength:1,
dataType: "json",
data: {
term: request.term,
rackid: "@Model.Rack.TMSRackID.ToString()",
},
success: function (data) {
response(data);
},
select: function (event, ui) {
return false;
},
create: function () {
$(this).data("autocomplete")._renderItem = function (ul, item) {
return $('<li>').data('item.autocomplete', item).append('<a>' + '<b>' + item.label + '</b><br>' + '<span style="color: #737373;font-size: 12px;font-weight: 300;line-height: 16px;font-family: Helvetica,Arial,sans-serif;white-space: owrap;">' + item.resourcename + ' | ' + item.customername + ' | ' + item.sitename + '<hr style="padding: 0px; margin: 0px;">' + '</span></a>')
.appendTo(ul);
};
}
});
},
});
});
以下操作方法,返回json: -
public ActionResult AutoComplete(string term,int? rackid)
{
var query = from techItems in ad
join resourcesItems in resources
on techItems.Technology.IT360ID.Value equals resourcesItems.RESOURCEID // join based on db2ID
orderby techItems.Technology.PartialTag
select new { label = techItems.Technology.Tag.ToString(), customername = resourcesItems.ResourceLocation.SiteDefinition.AccountDefinition.ORG_NAME.ToString(), resourcename = resourcesItems.RESOURCENAME.ToString(), sitename = resourcesItems.ResourceLocation.SiteDefinition.SDOrganization.NAME };
return Json(query, JsonRequestBehavior.AllowGet);
}
我面临的问题是自动填充结果将只包含一个标签,而不包含在创建部分中定义的连接字符串。
有人可以建议吗?,似乎我的.create没有任何效果。
由于
答案 0 :(得分:1)
我可以看到的一个问题是在您的配置中create
和select
选项传递给ajax设置而不是自动完成..所以请尝试
$("#ServerTag,#SDTag,#FirewallTag,#RouterTag,#SwitchTag").live("focus.autocomplete", null, function () {
var URL = "@Url.Content("~ / Server / AutoComplete ")";
$(this).autocomplete({
minLength: 1,
delay: 1000,
source: function (request, response) {
$.ajax({
url: URL,
cacheLength: 1,
dataType: "json",
data: {
term: request.term,
rackid: "@Model.Rack.TMSRackID.ToString()",
},
success: function (data) {
response(data);
}
});
},
select: function (event, ui) {
return false;
},
create: function () {}
});
//this should happen outside the ajax callback
$(this).data("autocomplete")._renderItem = function (ul, item) {
return $('<li>').data('item.autocomplete', item).append('<a>' + '<b>' + item.label + '</b><br>' + '<span style="color: #737373;font-size: 12px;font-weight: 300;line-height: 16px;font-family: Helvetica,Arial,sans-serif;white-space: owrap;">' + item.resourcename + ' | ' + item.customername + ' | ' + item.sitename + '<hr style="padding: 0px; margin: 0px;">' + '</span></a>')
.appendTo(ul);
};
});
答案 1 :(得分:0)