我有一個電子郵件的Rails形式,我想在Receiver字段中自動完成。下面是我做的:jQuery自動完成的Rails形式
= f.label :recipient_id, "Send to:"
= text_field_tag :recipient_id, nil
:javascript
$(function() {
$("#recipient_id").autocomplete({
source: "#{escape_javascript url_for(:action => 'recipient_autocomplete',
:controller => 'sent')}",
minLength: 2,
select: function(event, ui) {
$('input#recipient_id').attr('value', ui.item.value);
ui.item.value = ui.item.label
}
});
});
在我的控制,我有:
def recipient_autocomplete
@users= User.all(:conditions => ['first_name LIKE ? OR last_name LIKE?',
"%#{params[:term]}%", "%#{params[:term]}%"])
render :json => @users.collect {|x| {:label=>x.full_name, :value=>x.id}}.compact
end
JSON的呼叫工作正常,並顯示全名確定。這裏是元素:
<input type="text" name="recipient_id" id="recipient_id" class="ui-autocomplete-input"
autocomplete="off" role="textbox" aria-autocomplete="list"
aria-haspopup="true" value="158">
然而,當我點擊發送(相當於提交),該RECEIPIENT全名發送的,而不是receipient_id。
如何判斷Rails表單是否取得輸入字段的值(在上例中爲158)而不是其標籤?