开发者

radio button ruby on rails

I was wondering if there is a way to use radio button to set the value of a field that is a text box.

    <%= radio_button("demographics_questionaires", "gender", "Male")%> Male <br\>
    <%= radio_button("demographics_questionaires", "gender", "Female")%> Female <<br\>
    <%= radio_button("demographics_questionaires", "gender", "Other")%> 
    Other <%= f.text_field gender %> 

Above code is incorrect, but something on that lines.

If the other radio button is selected, I want gender to be set to the value of the text_field? Not sure how you do that association?

Ex.

[radio button] Male

[radio button] Female

[radio button] Other __________________

where ____________ is the text box to enter.

In the end If the user choose male, I want 开发者_JAVA百科the value the is stored in the database to be "Male", for female "FEMALE", or for other what the user inputs.

I kind of wanted to handle all the logic in the view. Is that possible or does it have to be done in the controller?

Any advice appreciated,


Check this code. It may help you

<%= form_for @user do |f| %>
  <%= f.radio_button :email, 'male' %> male<br />
  <%= f.radio_button :email, 'female' %> Female<br />
  <%= f.radio_button :email, 'others' %> others<br />
  <%= f.hidden_field :email  %>
<% end %>

jquery script

<script type="text/javascript">

$("input[type='radio']").change(function(){ 
    if ($('input[type="radio"]:checked').val() == "others")
    {
        $('input[type="hidden"]').val('')   
        $('input[type="hidden"]').prop("type", "text");
    }
    else if ($('input[type="radio"]:checked').val() != "others")
    {
        $('input[type="hidden"]').prop("type", "hidden");
        $('input[type="hidden"]').val($('input[type="radio"]:checked').val())
    } 
});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜