gpt4 book ai didi

javascript - Twitter Typeahead 返回结果的完整对象

转载 作者:行者123 更新时间:2023-12-03 05:25:25 24 4
gpt4 key购买 nike

我在使用 twitter typeaheadC# MVC 时遇到问题,当它自动完成时,它会将整个对象打印到我的输入中:

image

我的代码:

var states = [];

@foreach (var item in ViewBag.Clubes)
{
@:states.push({ "Nome" : "@item.Nome", "Id" : "@item.Id" })
}


$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1,
valueKey: 'Nome',
displayKey: 'Nome'
},
{
name: 'value',
source: substringMatcher(states),
templates: {

empty: [
'<div class="empty-message">',
' Clube não encontrado ',
'</div>'
].join('\n'),
suggestion: function (data) {
return "<strong>" + data.Nome + " " + data.Id + "</strong>";
}

}
});

下拉建议中的值工作正常,但是当我选择它们时,他用数组对象填充输入,有人知道我该如何解决它吗?

谢谢。

最佳答案

只需为预输入输入框设置一个值,如下所示

$('.typeahead').typeahead('val',data.Nome);

这是您的最终代码

var states = [];

@foreach (var item in ViewBag.Clubes)
{
@:states.push({ "Nome" : "@item.Nome", "Id" : "@item.Id" })
}


$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1,
valueKey: 'Nome',
displayKey: 'Nome'
},
{
name: 'value',
source: substringMatcher(states),
templates: {

empty: [
'<div class="empty-message">',
' Clube não encontrado ',
'</div>'
].join('\n'),
suggestion: function (data) {
return "<strong>" + data.Nome + " " + data.Id + "</strong>";
}

}
}).on('typeahead:selected', function(e, data) {
$('.typeahead').typeahead('val',data.Nome);
});

关于javascript - Twitter Typeahead 返回结果的完整对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41166998/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com