如何使表单字段与另一个字段相同?

时间:2013-07-08 20:54:03

标签: javascript html ruby-on-rails ruby forms

我有一个表单,允许用户编辑员工与公司的历史记录。

我希望在用户更改" To position"第一个条目的下拉菜单更改了"从位置"下一个条目中的下拉菜单等于它。

 <% f.fields_for :employee_position_histories do |emp_pos_his| %>
    <table class="crud-form">
      <tr>
        <td><%= f.label :from_position_id,  " Start Position: " %></td>
        <td><%= f.collection_select :from_position_id, Position.active, :id, :title, { :prompt => ' Select a Position ' }, { :class => 'clean average' } %></td>
      </tr>
      <tr>  
        <td><%= f.label :to_position_id,  " To Position: " %></td>
        <td><%= f.collection_select :to_position_id, Position.active, :id, :title, { :prompt => ' Select a Position ' }, { :class => 'clean average' } %></td>
      </tr> 
      <tr>
        <td><%= f.label :started_at, "Start Date: "%></td>
        <td><%= f.date_picker :started_at, :class => "clean average" %>
      </tr> 
      <tr>
        <td><%= f.label :ended_at, "End Date: "%></td>
        <td><%= f.date_picker :ended_at, :class => "clean average" %>
      </tr>
      <hr>
    </table> 
<% end %>

我不确定这样做的最佳方法。我的一位同事说使用jscript,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:0)

您可以使用jQuery执行此操作:

var fromBox = $("#from");
fromBox.change(function () {
    $("#to").val(fromBox.val());
});

只需将&#34;#替换为&#34; &#34;#from&#34;与你的ID

jsfiddle:http://jsfiddle.net/hummlas/m87ap/

答案 1 :(得分:0)

我不熟悉ruby语法,所以我不知道你选择id的用途 这是一种没有jQuery的方法:

var fromBox = document.getElementById("from");

fromBox.addEventListener('change',function() {
     document.getElementById("to").value = fromBox.value;
}, false);

Jsfiddle:http://jsfiddle.net/h5gew/12/