【发布时间】:2013-04-30 22:35:08
【问题描述】:
我正在使用 Bootstrap Typeahead 和 Ajax 构建一个特定的“自动完成”。
我的代码类似于:
searchInput.typeahead({
source: function (request, response) {
$.ajax({
url: '/Home/AllNamesAutoComplete',
type: 'post',
dataType: 'json',
data: { searchText: searchInput.val() },
success: function (data) {
var arr = [];
$.each(data.AutoCompleteItemViewModel, function () {
arr.push(this.Name + " - <span>" + this.Position + "</span>");
});
return response(arr);
}
})
},
items: 6
});
在推送中,我传递了两个不同的值:this.Name 和 this.Position。
一切正常,但如果我输入“S”作为第一个字母,它也会呈现<span>。我想将我的字符串作为 HTML 推送,但我仍然不知道该怎么做。
还有一件事是,当用户单击自动完成中的项目时,我希望仅选择 Name 而不是 Name 和 Position。
我想将 HTML 放入 Typeahead 源的数组中,然后我想在单击时在主输入中仅呈现特定值,而不是两者。
我查看了“Extend the bootstrap-typeahead in order to take an object instead of a string”,但我无法理解,因为它使用的是 Backbone。
【问题讨论】:
-
如果您不介意使用自定义 fork,可以尝试包含此功能的 my version。
标签: jquery twitter-bootstrap autosuggest