简体   繁体   中英

Dynamically changing select based on other param select in same form?

I have form that has a country and state params. Depending on the country chosen, I want the state to change to lessen the choices.

I have helpers for the states and countries

I tried:

<%= f.select(:country, options_for_select((countries), @stripe_account[:country]), {}, { class: "form-control input-lg" }) %>


  <div class="form-group">
     <%= f.label :address_state, "State/Province/Region" %>
     <div class="field <%= @stripe_account.country == 'US' ? 'present' : 'hidden' %>">
       <%= f.select(:address_state, options_for_select((us_states), @stripe_account[:address_state]), {}, { class: "form-control input-lg", id: "address_state us-states"}) %>
     </div>
     <div class= "field <%= @stripe_account.country == 'AU' ? 'present' : 'hidden' %>">
       <%= f.select(:address_state, options_for_select((au_regions), @stripe_account[:address_state]), {}, { class: "form-control input-lg", id: "address_state au_regions"}) %>
     </div>
...
</div>

This doesn't seem to work. And another thing I do not like about it, is it only hides the form but doesn't hide the style. So its just hidden and there is a blank area of space.

I have done stuff like this for other forms but seems to not work unless it's in HTML:

  $("#country").on("change",function(){"US"===$(this).val()?$("#banking-us, #banking-us-1").show():$("#banking-us, #banking-us-1").hide()});

I tried this with the rails form, but didn't seem to work.

All of the choices are based on helpers for the form such as:

def countries
  [
    ['US'],
    ['AU'],
  ]
end

How can I do this?

Try the Carmen plugin:

http://autonomousmachine.com/posts/2009/4/1/carmen-a-rails-plugin-for-geographic-names-and-abbreviations

You can also check this other stackoverflow post:

Ruby on Rails Country/State Select Enigma

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