【问题标题】:Unable to change input value after typeahead selection预输入选择后无法更改输入值
【发布时间】:2014-04-23 13:18:07
【问题描述】:

我在同一页面的 2 个输入中实现了预输入。一个工作完美无缺,但另一个有意想不到的行为。

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

输入上的每个键事件都会触发一个 js 错误: - Uncaught TypeError: undefined is not a function 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>

【问题讨论】:

    标签: javascript jquery typeahead.js


    【解决方案1】:

    我发现了问题。碰巧 displayKey 是 Invoice 类的一个 int 字段。它不工作。改成String,所有问题都解决了!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-25
      • 2021-03-17
      • 1970-01-01
      • 2020-11-08
      相关资源
      最近更新 更多