2013-03-29 42 views
0

繼續從this(其中一個employee belongs_to role並具有根據該角色的薪水)我想我New Employee form更具交互性有點不同的價值觀和IDS radio_buttons:我想salary字段的值根據從單選按鈕組中選擇的role進行更改。simple_form f.association爲:與jQuery的操縱

我的表單現在看起來是這樣的:

<%= simple_form_for(@room) do |f| %> 
    <%= f.input :name, label: 'Full Name', :required => true %> 
    <%= f.association :role, as: :radio_buttons, :required => true %> 
    <%= f.input :salary, label: 'Salary', :required => true %> 
    <%= f.button :submit %> 
<% end %> 

而且我employees.js.coffee文件中有這樣的代碼:

jQuery -> 
    $('#employee_employee_role_id_1').click -> 
    $(document.getElementById('employee_salary')).val(15) 

我在那裏的值硬編碼,以確保有一個與我的表單字段沒有問題IDS。

我需要的是有.click方法適用於所有#employee_employee_role_id_X領域(其中X是每個單選按鈕的數量),並傳遞給.VAL()的值是role.salary

不過,作爲新手我對Rails和js/jQuery都很無語。

回答

0

好的,我設法完成了這項工作,但我並不確定這是最好的方法。這裏所說:

在形式,我只是說value_method: :salary

<%= f.association :role, as: :radio_buttons, value_method: :salary, :required => true %> 

,並在我的.js.coffee文件:

jQuery -> 
    $("input[name='role[role_id]']").change -> 
    $(document.getElementById('salary')).val(($("input:radio[name='role[role_id]']:checked").val())) 

請隨意發表評論,或者即使存在一個提供一個更好的答案! :)