无法在预先键入选择后更改输入值

Unable to change input value after typeahead selection

本文关键字:输入 选择      更新时间:2023-09-26

我在同一页面中的两个输入中实现了typeahead。其中一个工作完美,但另一个有着意想不到的行为。

我只能使用TypeAhead一次,一旦我选择了一个建议,输入字段就会填充正确的值,但我现在无法更改它的值。我来解释一下:

输入上的每个键事件都会引发一个js错误:-Uncaught TypeError:undefined不是函数typeahead。js:944

return (str || "").replace(/^'s*/g, "").replace(/'s{2,}/g, " ");

然后,一旦输入失去焦点,修改后的值就会恢复为预先输入的选定值。

希望这是我代码的相关部分:

<input type="text" class="form-control text-center number" id="document" name="document" >
<script>
var invoices = new Bloodhound({
    remote : "invoice/listInvoices?action=typeahead&q=%QUERY",
    limit : 15,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    datumTokenizer : Bloodhound.tokenizers.whitespace("invoice")
});
invoices.initialize();
var invoiceTemplate = Handlebars.compile("<p>{{invoice}} - {{cusName}} - &#36;{{value}}</p>");
$(document).ready(function() {
    $("#document").typeahead({
        minLength : 3,
        hint : false,
        highlight : true
    },{
        items : 15,
        displayKey : "invoice",
        source : invoices.ttAdapter(),
        templates : {
            empty : ["No Matches Found!"].join("'n"),
            suggestion : invoiceTemplate
        }
    });
    $("#document").bind("typeahead:selected", function(e, datum, name) {
        $.ajax(
        {
            url : "invoice/listinvoices.jsp?document=" + datum.invoice,
            success : function(data, textStatus, jqHXR)
            {
                $("#list").html(data);
            }
        });
    });
</script>

我发现了问题。displayKey恰好是Invoice类中的一个int字段。它不起作用。更改为字符串,所有问题都消失了!