未调用Autocomplete formatItem,firebug中也没有任何错误
Autocomplete formatItem not called, neither any error in firebug
基本上我想显示一个图像,除此之外还有文本。这是我的自动完成代码:
$("#tagBox").autocomplete({
source: '/Friends/FriendsTagHandler.aspx?FileID=<%=Request.QueryString["FileID"] %>',
scroll: true,
scrollHeight: 300,
formatItem: function (data, i, n, value) {
console.log(values);
var values = value.split(".");
return "<img src='/images/ProfileAvatar/ProfileImage.aspx?AccountID=" + values[0] + "'/> " + values[1];
},
formatResult: function (data, value) {
console.log(value);
return value.split(".")[1];
}
});
然而,我的formatItem或formatResult既没有被调用,也没有在firebug控制台中得到任何错误。
更新:我在SO本身的某个地方读到formatItem被弃用,我们应该从服务器本身返回格式化的数据。所以我从我的服务器返回了格式化数据:
代码段
foreach (var item in friends)
{
sb.Append("<img src='/images/ProfileAvatar/ProfileImage.aspx?AccountID=" + item.AccountID.ToString() + "'/>" + item.FirstName + " " + item.LastName).
Append(Environment.NewLine);
}
//context.Response.ContentType = "text/plain";
context.Response.Write(sb.ToString());
当我在浏览器中点击url时,我可以正确地看到图像和除此之外的名称。然而,自动完成框中并没有真正显示任何内容。
您得到的是jQueryUI自动完成,而它的祖先jQuery自动完成混淆了。jQueryUI自动完成没有formatItem
、formatResult
、scroll
或scrollHeight
选项。
为了完成你想要的,你需要覆盖_renderItem
函数,比如这个例子:
$("#tagBox").autocomplete({ ... })
._renderItem = function (ul, item) {
// Custom item display logic here.
};
此外,您的source
数据应该是一个返回数据的函数、数据本身或以以下格式返回数据的URL:
- 字符串数组:
['option1', 'option2', 'option3']
,或 - 属性为
label
或value
(或两者兼有(的对象数组:[{ label: 'option1', value: 'option1'}, { ... }]
首先,我在Jquery UI中没有看到任何formatItem
或"formatResult",下面是我所做的操作,并确保您返回JSON对象
When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL
http://jqueryui.com/demos/autocomplete/#remote-jsonp
$("#tagBox").autocomplete({
source: "@Url.Action("Search", "Person")",//I'm using asp.net MVC here
select: function (event, ui) {
$(this).val( ui.item.Name );
return false;
}
}).data("autocomplete")._renderItem = function (ul, item) {
var term=$(#tagBox).val();
return $("<li style='"background-color:Yellow'" ></li>")
.data("item.autocomplete", item)
.append("<a>"+ "<img src=/images/ProfileAvatar/ProfileImage.aspx?thumbnailId="+item.ImageId +"'></img>" + item.Name.replace(new RegExp('(' + item.Term + ')', 'gi'), "<b>$1</b>") + "</a>").appendTo(ul);
};
我的控制器代码
IList<Person> people= new List<Person>();
// people.Add() here
return Json(people);
还有我的个人课
public class Person
{
public int Id{ get; set; }
public string Name { get; set; }
public int ImageId
{
get;
set;
}
}
- javascript函数,该函数不起作用,但不会显示任何错误
- 找不到在 create-js 中运行的动画,未列出任何错误
- JQuery停止工作,没有任何错误消息
- JS-为什么这不起作用-任何错误
- melonjsapi不会加载我的地图,也不会收到任何错误
- 加载json文件时出现空白网页,没有任何错误
- jQuery 表单验证不会引发任何错误
- 使用 sailsjs 护照身份验证本地策略既不会给出任何错误,也不会对用户进行身份验证
- javascript不起作用,但IE没有显示任何错误
- 我的 webgl 着色器程序有什么问题:它在控制台中没有抛出任何错误
- 简单的nicEdit根本无法正常工作,没有任何错误
- 当没有任何错误时,Javascript给了我一个错误
- 谷歌图表不播放,不显示任何错误
- 如何在应用程序的多个组件中维护一个 js 文件而不会出现任何错误
- 尝试使用jQuery向ajax函数自动提交表单失败,没有任何错误
- html5视频元素有任何错误?它不会在几个小时后继续播放内容
- 主干:任何错误都会触发事件
- AngularJS ng控制器指令不接受javascript中的变量(作用域函数),也不给出任何错误
- AngularJS路由不工作,但没有抛出任何错误
- $routeChangeSuccess的奇怪行为:第一次加载时没有触发(但没有抛出任何错误)