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

标签 javascript model-view-controller twitter autocomplete typeahead

我在使用 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/

相关文章:

javascript - 在不刷新页面的情况下将变量从 JavaScript 传递到 PHP

javascript - backbone.js 中的嵌套模型

java - 复杂 GUI 程序中的 MVC 模式

java - 相同(?)对象的不同对象引用

c# - TweetInvi 视频上传失败,错误代码 "The tweet cannot be published as some of the medias could not be published!"

javascript - Jquery Twitter 搜索 api - 没有结果

api - 推特 API : allow authenticated user to post tweets on another user's behalf

javascript - 嵌套函数如何在稍后访问 Javascript 中的变量而无需声明?

javascript - 为什么 Object.defineProperty(Object.prototype, "propname", {.., 值 : fn} promotes fn to be a global variable?

javascript - 有没有办法在模式中有条件地定义 'required' 键?