BootstrapTypeaheadと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
});
プッシュでは、2つの異なる値を渡します:this.Name
とthis.Position
。
すべて正常に機能しますがS
、最初の文字として「」と入力すると、もレンダリング<span>
されます。文字列をHTMLとしてプッシュしたいのですが、それを行う方法がまだわかりません。
もう1つは、ユーザーがオートコンプリートのアイテムをクリックしたときに、Name
とではなく、とName
だけを選択したいということですPosition
。
Typeaheadのソースの配列にHTMLを配置し、クリックすると、メイン入力に特定の値のみをレンダリングし、両方をレンダリングしたくありません。
「文字列の代わりにオブジェクトを取得するためにブートストラップタイプアヘッドを拡張する」を確認しましたが、Backboneを使用しているため理解できません。