2013-11-28 49 views
0

這是使用rails和產品散列typeahead如何使用typeahead在自動完成期間返回其他值?

def autocomplete 
    render json: Product.ac_search(params[:query].to_s) 
    end 

$ -> 
    $('#order_id').typeahead 
    name: "name" 
    valueKey: "name" 
    remote: "/products/autocomplete?query=%QUERY" 

= simple_form_for @order do |form| 
    = form.input :product_id, as: :string 

比如我實現自動完成的:

[{"id":"8004","name":"cow leg"}, {"id":"8004","name":"dog leg"}] 

如何通過返回按名稱選擇項目,如果產品的ID?

+0

插件,你使用的是什麼預輸入....應該有一個'select'事件回調,它提供了訪問項目的選擇 – charlietfl

+0

我我正在使用http://twitter.github.io/typeahead.js/ – tomekfranek

回答

1

嘗試在插件文檔概括這個...使用自定義事件:

$('#order_id').on('typeahead:selected',function(evt,data){ 
    alert(data.id); 
}) 
+0

以及如何將此data.id設置爲字段值? – tomekfranek

+0

'$(selector).val(data.id)' – charlietfl

相關問題