1

typeahead.jsを使用して、非表示の html フォーム フィールドに製品 ID を割り当てています。これは、一致がある場合にうまく機能します。

$('.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)

入力フィールドが空白のままの場合、非表示フィールドをクリアします。

$('.products_tt .typeahead').blur ->
    if $(this).val().length == 0
        $('#disk_file_product_id').val("")

ただし、入力フィールドにテキストが入力されているが一致しない場合は、非表示フィールドもクリアする必要があります。

Java/Coffeescript のスキルが弱いので、これを行う方法がわかりません?!?

4

2 に答える 2

2

これは数か月前のものですが、これを行う方法は次のとおりです。

jQuery を使用して、各 keyup イベントで DOM にある ".tt-suggestions" の数を調べます。何もない場合は、隠しフィールドをクリアします。

$('.products_tt .typeahead').typeahead({
    name: 'products_tt',
    prefetch: url: '/products.json',
    ttl: 60 * 5e3
    template: '<p><strong>{{value}}</strong> – {{year}}</p>',
    engine: Hogan
}).on("typeahead:selected typeahead:autocompleted", function (e, datum) {
    $('#disk_file_product_id').val(datum.id);
}).on('keyup', function () {
    if($('.tt-suggestion').length === 0){
        $('#disk_file_product_id').val("");
    }
});
于 2014-01-07T02:20:21.917 に答える