简体   繁体   English

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

[英]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.

相关问题 将文本添加到另一个字段后,如何使字段增加0.5 - How do I make a field increment by .5 after text is added to another field 如何进行现场自动对焦? - How do I make a field autofocus? 如何使表单字段中的负数产生警告错误 - How do I make a negative amount in form field produce a warning error 我如何才能使我的按钮成为具有相同信息的弹出字段而不是将其定向到不同的页面? - How do i make it so that my button becomes a pop field with same information instead of directing it into a different page? 在下一个最后一个字段上制作新的相同表格 - Make new same form on next last field 将html中的字段计算传递给SAME表单中的另一个字段 - Pass a field calculation in html to another field in the SAME form 如何基于复选框设置另一个表单字段? - How can i set another form field based on a checkbox? 如何以与更改另一个输入字段相同的数量来更改一个输入字段? - How can I change one input field in the same amount I change another input field? 当表单字段的名称带有方括号时,如何使用Javascript强制将光标移至特定表单字段? - How do I use Javascript to force cursor to specific form field when form field has a name with brackets? 如何使用javascript / jQuery获取表单字段值以求和另一个字段值? - How can I get form field values to sum up to another field value using javascript/jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM