我正在使用预输入搜索,当我搜索并选择一个项目时,所选项目将从字段中消失。如何确保该值不会从字段中消失?
$.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>
我也遇到过同样的情况。
预先输入文档对此进行了更好的解释,但您应该使用预先输入设置器
.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