[英]How do I make a form field the same as another field?
I have a form that allows a user to edit an employee's history with the company. 我有一个表格,允许用户编辑公司的员工历史。
I want to make it so when the user changed the "To position" dropdown menu of the first entry it changes the "From position" dropdown menu in the next entry to be equal to it. 我要这样做,以便当用户更改第一个条目的“到位置”下拉菜单时,它将下一个条目中的“从位置”下拉菜单更改为与之相等。
<% 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 %>
I am unsure of the best way to do this. 我不确定执行此操作的最佳方法。 A colleague of mine said to use jscript but I am not sure how to do that either. 我的一位同事说要使用jscript,但我也不知道该怎么做。
You can use jQuery to do this: 您可以使用jQuery来做到这一点:
var fromBox = $("#from");
fromBox.change(function () {
$("#to").val(fromBox.val());
});
just replace the "#to" and "#from" with your ID's 只需用您的ID替换“ #to”和“ #from”
jsfiddle: http://jsfiddle.net/hummlas/m87ap/ jsfiddle: http : //jsfiddle.net/hummlas/m87ap/
I'm unfamiliar with ruby syntax so I can't tell what you use for your select ids. 我不熟悉ruby语法,所以无法告诉您选择ID所用的内容。
Here's a way to do it without jQuery: 这是没有jQuery的一种方法:
var fromBox = document.getElementById("from");
fromBox.addEventListener('change',function() {
document.getElementById("to").value = fromBox.value;
}, false);
Jsfiddle: http://jsfiddle.net/h5gew/12/ Jsfiddle: http : //jsfiddle.net/h5gew/12/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.