如何从另一个输入文本字段中输入的值将值设置为输入隐藏字段

时间:2020-04-26 23:38:11

标签: javascript jquery ruby-on-rails ruby

我要获取此表单名称并隐藏表单名称

<input placeholder="Nome" class="form-control" type="text" 
name="morning[name]" 
id="morning_name">

隐藏字段

<input placeholder="Nome" hide_label="true" required="required" 
type="hidden" 
name="morning[foots_attributes][0][name]" 
id="morning_foots_attributes_0_name">

我的bootstrap_form

<%= bootstrap_form_with(model: @morning, url: morning_path, local: true) do |form| %>

<%= form.text_field :name, placeholder: "Nome", hide_label: true%>

<%= form.fields_for :foots do |foot| %>
<%= foot.hidden_field :name, placeholder: "Nome", hide_label: true, required: true %>   
<% end %>

<%= form.submit "Registrar horário", class:"btn btn-primary", data: { disable_with: 
'Registrando....' } %>

<% end %>

<%= javascript_pack_tag 'custom' %>

1 个答案:

答案 0 :(得分:1)

您可以使用jQuery .change()完成此操作。

$("#morning_name").change(function(){
    $("#morning_foots_attributes_0_name").val(this.value);
});

https://api.jquery.com/change/