选择值后,输入值从字段中消失 - JS

问题描述 投票:0回答:1

我正在使用预输入搜索,当我搜索并选择一个项目时,所选项目将从字段中消失。如何确保该值不会从字段中消失?

$.get("searchProducts", function (data) {
    $("#product-search").typeahead({
        "items": "all",
        "source": data,
        "autoSelect": false,
        displayText: function (idata) {
            return idata.quantity;
        },
        updater: function (idata) {

          ("#product-search").val(idata.name);
          ("#name").val(idata.name);



        }
    });
});

<input type="text" autocomplete="off" name="product_search" id="product-search" class="form-control" required>

<input type="text" name="name" id="name" class="form-control" required>
javascript typeahead
1个回答
0
投票

我也遇到过同样的情况。

预先输入文档对此进行了更好的解释,但您应该使用预先输入设置器

.typeahead('val', value)
而不是
jQuery.val()

//wrong ("#product-search").val(idata.name);

("#product-search").typeahead('val', idata.name);;

点击链接了解更多信息。 https://github.com/twitter/typeahead.js/blob/master/doc/jquery_typeahead.md#jquerytypeaheadval-val

© www.soinside.com 2019 - 2024. All rights reserved.