0

这个继续(employee belongs_to role根据该角色,并且有薪水)我想让我New Employee form的交互性更强:我希望该salary字段的值根据role从单选按钮组中选择的值而改变。

我的表格现在看起来像这样:

<%= 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)

我将值硬编码以确保我的表单字段ID没有问题。

我需要的是让该.click方法适用于所有#employee_employee_role_id_X字段(其中 X 是每个单选按钮的编号),并且传递给 .val() 的值是role.salary

但是,作为 Rails 和 js/jQuery 的新手,我一无所知。

4

1 回答 1

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()))

如果存在,请随时发表评论,甚至提供更好的答案!:)

于 2013-03-30T11:28:12.840 回答