使用Rails 3.2和Rails表单-当单选按钮为true时,不知道如何显示输入文本字段,否则将其隐藏 [英] Using Rails 3.2 and Rails form - don't know how to display an input text field when a radio button is checked true and hide it otherwise

查看:60
本文介绍了使用Rails 3.2和Rails表单-当单选按钮为true时,不知道如何显示输入文本字段,否则将其隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里获取了几则帖子,但仍然无法使它正常工作. 当其它"单选按钮被选中,我想一个f.text_field露面,当被选中时被隐藏. 之后,我希望能够为thr属性f.client_consent_refusal_reason输入文本值.

I fetched couple posts over here but I am still unable to make it work. When "Other" radio button is checked I would like a f.text_field to show up and when is unchecked to be hidden. I would like after that to be able to input a text value for thr attribute f.client_consent_refusal_reason.

这是我的HTML:

<table>
  <tr>
    <td>
      <%= form_for(@client, :url => {:controller => 'client', :action=> :outcome_consent_complete, :id => @client.id }) do |f| %>
        <%= f.radio_button :client_consent_refusal_reason, 'Hearing loss', :checked => false %>
        <%= f.label :radio_button_label, 'aaa' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'bbb', :checked => false %>
        <%= f.label :radio_button_label, 'ccc' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'ddd', :checked => false %>
        <%= f.label :radio_button_label, 'eee' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'fff', :checked => false %>
        <%= f.label :radio_button_label, 'ggg' %>
        <br>
        <%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false %>
        <%= f.label :radio_button_label, 'Other' %>
        <%= f.text_field :client_consent_refusal_reason %>
        <br><br>
        <div class="actions">
          <%= f.submit 'Submit' %>
        </div>
      <% end %>
    </td>
  </tr>
</table>

当任何上述单选按钮的被检查领域client_consent_refusal_reason应设置为相关联的值,但被选中时其他,我想能够捕获对应的值.

When any of the above radio buttons are checked the field client_consent_refusal_reason should be set to the associated value, but when Other is checked, I would like to be able to capture the correspondent value.

我不知道如何解决这个问题.

I don't know how to solve this problem.

推荐答案

我替换了这段代码:

<%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false %>
<%= f.label :radio_button_label, 'Other' %>
<%= f.text_field :client_consent_refusal_reason %>

与此:

<%= f.radio_button :client_consent_refusal_reason, 'Other', :checked => false,
    :onchange => "$('other_reason_input')[this.checked ? 'show' : 'hide']();" %>
<%= f.label :radio_button_label, 'Other (please list): ' %>

<div id="other_reason_input" style="display:none;">
  <%= f.text_field :client_consent_refusal_reason_other %>
</div>

在模型类中,我必须添加以下行:

And in the model class I had to add these lines:

  attr_accessor :client_consent_refusal_reason_other

  def client_consent_refusal_reason_other=(value)
    self.client_consent_refusal_reason = value if client_consent_refusal_reason == 'Other'
  end

  def client_consent_refusal_reason_other
    client_consent_refusal_reason
  end

现在一切正常.

这篇关于使用Rails 3.2和Rails表单-当单选按钮为true时,不知道如何显示输入文本字段,否则将其隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆