简体   繁体   中英

Dynamic select in nested form rails

How do js get the selected value in a dropdown and pass it to the controller so that it returns a list of names for the other dropdown?

What I've done so far: salmonellas.js

$(function () {
   $('body').on('change', "#mode_change", function () {
        var selectedText = $(this).find("option:selected").text();
        var selectedValue = $(this).val();
        if (selectedText) {
          $.get('/salmonellas/find_stages?mode_title='+ selectedText, function(selectedText) {                
            return $(selectedText).html();
          });
        }
    });
});

salmonellas/form.html.erb

<div class="process_salmonellas">
    <% f.simple_fields_for :process_salmonellas do |process_salmonella| %>
        <%= render 'process_salmonella_fields', :f => process_salmonella %>
    <% end %>
</div>

salmonellas/_process_salmonella_fields.html.erb

<div class="mode_change" id="mode_change">
    <%= f.collection_select :title, Mode.order(:name), :id, :name, class: 'mode_change', id:'mode_change', include_blank: true, "data-content": :mode_id%>
</div>
<h4>Stage</h4>
<div class="stage">
    <% f.simple_fields_for :stages do |stage| %>
        <%= render 'stage_fields', :f => stage %>
    <% end %>
</div>

salmonella/_stage_fields.html.erb

<div class="form-inputs">
    <%= f.grouped_collection_select :title, Mode.order(:name), :steps, :name, :id, :name, class: "step_selection" %>
</div>

salmonellas_controller.rb

def find_stages
    @mode = Mode.where("name = ?", params[:mode_title])
    @steps = @mode.steps
end

Is looking for another model, why the fields have to be pre-registered. I'm using cocoon to let it nested.

Updating

salmonellas/_process_salmonella_fields.html.erb

<div class="form-inputs">
    <%= f.collection_select :title, Mode.order(:name), :id, :name, class: 'mode_change', id:'mode_change', include_blank: true, "data-content": :mode_id%>
</div>
<h4>Stage</h4>
<div class="stage">
    <% f.simple_fields_for :stages do |stage| %>
        <%= render 'stage_fields', :f => stage %>
    <% end %>
</div>

Updating 2

salmonellas/_process_salmonella_fields.html.erb

<div class="form-inputs">
    <%= f.collection_select :title, Mode.order(:name), :id, :name, id:'mode_change', include_blank: true %>
</div>

salmonellas.js

$(function () {
   $('body').on('change', ".mode_change", function () {
        var selectedText = $(this).find("option:selected").text();
        var selectedValue = $(this).val();
        if (selectedText) {
          $.get('/salmonellas/find_stages?mode_title='+ selectedText, function(selectedText) {                
            return $(selectedText).html();
          });
        }
    });
});

Updating 3

salmonellas.js

 $(function () {
   $(document).on('change', "#mode_change", function () {
        var selectedText = $(this).find("option:selected").text();
        var selectedValue = $(this).val();
        alert("Selected Text: " + selectedText);
    });
});

IDs must be unique on your DOM. You are duplicating them for div and select tag. First change them and use this to get the value of selected option

$(document).on('change', ".mode_change", function () {
    var selectedText = $(this).find("option:selected").text();
    var selectedValue = $(this).val();
      ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM