JQuery:Twitter Typeahead功能不匹配

Mel*_*emi 1 javascript jquery ruby-on-rails coffeescript typeahead.js

我正在使用typeahead.js将产品ID分配给隐藏的html表单字段.当匹配时,这很有用:

$('.products_tt .typeahead').typeahead
    name: 'products_tt',
    prefetch: 
        url: '/products.json',
        ttl: 60 * 5e3
    template: '<p><strong>{{value}}</strong> – {{year}}</p>',
    engine: Hogan

$('.products_tt .typeahead').on "typeahead:selected typeahead:autocompleted", (e,datum) ->
    $('#disk_file_product_id').val(datum.id)
Run Code Online (Sandbox Code Playgroud)

当输入字段留空时,我清除隐藏字段:

$('.products_tt .typeahead').blur ->
    if $(this).val().length == 0
        $('#disk_file_product_id').val("")
Run Code Online (Sandbox Code Playgroud)

但是我还需要在输入字段中输入文本时清除隐藏字段,但是没有匹配.

我的Java/Coffeescript技能很弱,所以不确定如何做到这一点?!?

Nit*_*ked 5

以下之一应该工作:

1)首先进入输入字段的change事件,然后清除#disk_file_product_id.typeahead如果有选择,您的活动将在稍后再次设置此项.这也可以节省您的blur回调.

$('.products_tt .typeahead').change ->
    $('#disk_file_product_id').val("")
Run Code Online (Sandbox Code Playgroud)

2)清除#disk_file_product_idopenedclosed事件,而不是在该领域的change事件.我不喜欢这个.

$('.products_tt .typeahead').on "typeahead:opened typeahead:closed" ->
    $('#disk_file_product_id').val("")
Run Code Online (Sandbox Code Playgroud)