【问题标题】:Autocomplete with hidden id field doesn't work带有隐藏 ID 字段的自动完成功能不起作用
【发布时间】:2017-05-17 15:42:14
【问题描述】:

我在另一篇帖子 (How to get JQuery UI Autocomplete work with item id) 之后执行了此自动完成功能,它对自动完成功能运行良好。问题是它没有得到 id。

这是我的代码:

var raw = @Html.Raw(Json.Encode(@ViewBag.CT));
var source = [];
var mapping = {};
for (var i = 0; i < raw.length; ++i) {
    source.push(raw[i].procedure);
    mapping[raw[i].procedure] = raw[i].id;
}

$('#tags').autocomplete({
    minLength: 1,
    source: source,
    select: function (event, ui) {
        $('#tagsID').val(mapping[ui.item.id]);
    }
});

数组包含 4147 个元素。

提前谢谢你。

【问题讨论】:

  • 控制台有错误吗?
  • @Alexandru-IonutMihai -> 不,我没有。

标签: javascript jquery arrays asp.net-mvc-4 autocomplete


【解决方案1】:

我不知道为什么,但它只适用于 value 而不是 id...所以最终的代码是这样的:

   var raw = @Html.Raw(Json.Encode(@ViewBag.CT));

var i;
for(i = 0; i < raw.length; i++){
    raw[i].value = raw[i]['id'];
    delete raw[i].id;
}

var source = [];
var mapping = {};
for (var i = 0; i < raw.length; ++i) {
    source.push(raw[i].procedure);
    mapping[raw[i].procedure] = raw[i].value;
}

$('#tags').autocomplete({
    minLength: 1,
    source: source,
    select: function (event, ui) {
        $('#tagsID').val(mapping[ui.item.value]);
    }

希望它不要让应用程序太慢..

还是谢谢你。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多