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

Posted

技术标签:

【中文标题】如何从另一个输入文本字段Javascript中输入的值将值设置为输入隐藏字段【英文标题】:how to set the value to a input hidden field from value entered in another input text field Javascript 【发布时间】:2020-08-10 10:58:02 【问题描述】:

我想得到这个表单名并把表单名隐藏起来

<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】:

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

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

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

【讨论】:

以上是关于如何从另一个输入文本字段Javascript中输入的值将值设置为输入隐藏字段的主要内容,如果未能解决你的问题,请参考以下文章