【问题标题】:Select onchange with city-state ruby gem使用城邦红宝石宝石选择 onchange
【发布时间】:2015-09-17 00:25:02
【问题描述】:

我正在尝试使用城市状态 gem (https://github.com/loureirorg/city-state) 并使城市选择器可以使用 Javascript 动态更新取决于状态选择器。

我的想法:选择州后,“form.select :city”将仅填充该州的城市。

这是我的 ...html.erb

<%= fs.select :state_code, options_for_select(us_states), {}, class: "form-control", id: "state_code", :onchange => "javascript: state_code();"  %>

<%= fs.select :city, options_for_select(CS.get :"#city", :al), {},   class: "form-control", id: "city" %>

这是我的 Javascript:

   <script type="text/javascript">
     $("#state_code").onchange( function() {
      $("#city").val("state_code").change();
 })
</script>

【问题讨论】:

    标签: javascript html ruby-on-rails-4 onchange


    【解决方案1】:

    您需要修改您的 HTML 填充和 JS 代码。此外,应用程序控制器中的一个操作用于响应获取该州城市的请求。

    ---
    JS
    ---
    $(document).on('change', '#states-of-country', function(e) {
      var cities_of_state, input_state, state;
      input_state = $(this);
      cities_of_state = $('#cities-of-state');
      state = this.options[e.target.selectedIndex].id;
      if (state === 'no-state') {
        return cities_of_state.html('');
      } else {
        $.ajax({
          url: '/cities/' + $(this).children(':selected').attr('id'),
          success: function(data) {
            var opt;
            opt = '<option value="" selected>Select Your City</option>';
            if (data.length === 0) {
    
            } else {
              data.forEach(function(i) {
                opt += '<option value="' + i + '">' + i + '</option>';
              });
              cities_of_state.html(opt);
            }
          }
        });
      }
    });
    ------------------------
    Application Controller:
    ------------------------
    
    def cities
      render json: CS.cities(params[:state], :us).to_json
    end
    
    -----------
    routes.rb:
    -----------
    get 'cities/:state', to: 'application#cities'
    -----
    HTML
    -----
    <select id="states-of-country" name="project[state]">
      <option id="no-state" value="" selected>Select Your State</option>
      
      /* For states within US */
      <% CS.states(:us).each do |key, value| %>
        <option value="<%= value %>" id="<%= key %>"><%= value %></option>
      <% end %>
    </select>
          
    <select id="cities-of-state" name="project[city]" class="form-control form-control-small" required>
      <option value="" selected>Select Your City</option>
    </select>
                
                
                
                

    因此,当值更改时,将向服务器发出 GET 请求。然后在查看州代码时,它将返回属于该特定州的城市列表。 在 AJAX 取得成功后,这些城市正在人满为患。

    【讨论】:

    • 感谢您的回答!这可以作为 html 选择器。你知道如何使用 吗?我正在尝试作为更新属性的一部分提交并在帐户中显示以前选择并保存在数据库中的内容
    • 其实我一开始也试过,但是没搞明白。在那之后,我想到了一个技巧,将 select 元素命名为其他元素在 form_for 中的前缀。因此,我在 select 元素中手动编写了 name="project[state]",其中 form_for 用于 @project 对象。 .希望这个 hack 也对你有用。
    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    相关资源
    最近更新 更多