show rest of a form if a checkbox is ckecked in ruby on rails

前端 未结 6 982
终归单人心
终归单人心 2020-12-16 08:58

I need to ask to my user if will pay a service with credit card...if it checked the option pay_with_card? it must show the rest of the form, that ask for other data like car

相关标签:
6条回答
  • 2020-12-16 09:15

    You can do it through jQuery, for example:

    $ ->
      $('select#pay_with_card').change ->
        if $(this).val() == 'yes'
          $('.card_block').slideDown('fast')
        else
          $('.card_block').slideUp('fast')
    

    assumed that part of the form with payment card is included in the div with .card_block class

    0 讨论(0)
  • This worked for me with a form_with model and bootstrap
    Change my_hidden_form with an id that makes sense for your form.
    Original code is haml

    = form_with scope: :model, url: models_path, local: true do |form|
                .row
                  .col-6
                    .form-group
                      %h5.mb0 THE CASE TO TICK
                      = form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }}
    
                .row
                  .col-6
                    .form-group.collapse#my_hidden_form
                      %h5.mb0 THE FORM TO SHOW WHEN CASE IS TICKED
                      = form.text_field :name, placeholder: "A name"
    
                .row
                  .col-md-12.text-right
                    = form.submit 'Submit', class: "btn btn-primary"
    

    Converted to erb/html with https://haml2erb.org/

    <%= form_with scope: :model, url: models_path, local: true do |form| %>
    <div class="row">
      <div class="col-6">
        <div class="form-group">
          <h5 class="mb0">THE CASE TO TICK
            <%= form.check_box :form_value, {:data => {:aria => {controls: :my_hidden_form, expanded: false}, :toggle => "collapse", :type => "checkbox", :target => "#my_hidden_form" }} %>
          </h5>
        </div>
      </div>
    </div>
      <div class="row">
        <div class="col-6">
          <div class="form-group collapse" id="my_hidden_form">
            <h5 class="mb0">THE FORM TO SHOW WHEN CASE IS TICKED
              <%= form.text_field :name, placeholder: "A name"  %>
            </h5>
          </div>
        </div>
      </div>
    <div class="row">
      <div class="col-md-12 text-right">
        <%= form.submit 'Submit', class: "btn btn-primary" %>
        </div>
      </div>
    <% end %>
    
    0 讨论(0)
  • 2020-12-16 09:24

    Make the card number/mail details div style="display:none;", then add some javascript to the checkbox to change it to display:block;

    Something like this:

    <%= form_for(@product) do |f| %>
      <%= f.label :pay_with_card? %>
      <%= f.check_box :pay_with_card,{}, "Yes", "No"%>
      <div id="card_details" style="display:none;">
        <%= f.label :card_number %> <%= f.text_field :card_number %>
        <%= f.label :mail %> <%= f.text_field :mail %>
      </div>
    <% end %>
    <script type="text/javascript">
      var checkbox = document.getElementById('product_pay_with_card');
      var details_div = document.getElementById('card_details');
      checkbox.onchange = function() {
         if(this.checked) {
           details_div.style['display'] = 'block';
         } else {
           details_div.style['display'] = 'none';
         }
      };
    </script>
    
    0 讨论(0)
  • 2020-12-16 09:25

    Ok my solution is this: all the code in the view, if a user check pay_with_card...(mi code is in spanish) it shows the complete form...if is not checked don´t show nothing, just the same checkbox asking for payment... thanks guys.

    function mostrar (){
    var checkbox = document.getElementById('chk_tarjeta');
    if (checkbox.checked)
    document.getElementById("card_details").style.display = "block";
    else
    document.getElementById("card_details").style.display = "none";
    </script>
    
    <h1>Forma de Pago</h1>
    <%= form_for(@product) do |f| %>
    <div id="product_pay_with_card">
    <div >
    <%= f.label :paga_con_tarjeta? %></br>
    <%= f.check_box :paga_con_tarjeta, :id => "chk_tarjeta", :onclick => "mostrar();" %>
    <div>
    </div>
    </div>
    <div id="card_details" >
    <div>
    <%= f.label :numero_de_tarjeta %></br>
    <%= f.text_field :numerotarjeta %>
    </div>
    <div>
    <%= f.label :codigo_de_seguridad %></br>
    <%= f.text_field :codigoseguridad %>
    </div>
    
    0 讨论(0)
  • 2020-12-16 09:40

    You can use JS for it or move pay_with_card out of form like:

    <%= link_to 'pay with card', your_current_path(:pay_with_card => 1) %>
    <%= form_for(...) do |f| %>
      <% if params[:pay_with_card] %>
        <%= # fields for card %>
      <% end %> 
    <% end %>
    
    0 讨论(0)
  • 2020-12-16 09:41

    How about using jQuery?

    First, wrap your credit card fields in a div with class credit_card_fields and than add this JS code to your page:

    $("input[type='checkbox']#pay_with_card").on('change', function(){
      $('.credit_card_fields').toggle();
    });
    
    0 讨论(0)
提交回复
热议问题