Rails / jQuery动态选择菜单-应用于全部

时间:2019-07-19 13:37:33

标签: jquery ruby-on-rails

我应该开始,如果可以使用纯净JS更好地做到这一点,我会不厌其烦,但这主要适用于jQuery。

我所做的是使用Rails和jQuery创建一个动态选择菜单,如果选择了特定区域,则将出现关联的位置。

这是我的看法:

.form-group
= f.label :region_id, class: 'col-sm-2 control-label'
.col-sm-10
  = f.collection_select :region_id, Region.order(:name), :id, :name, include_blank: true
#location_select.form-group
 = f.label :location_id, class: 'col-sm-2 control-label'
 .com-sm-10.form-select
   = f.grouped_collection_select :location_id, Region.order(:name), :locations, :name, :id, :name, class: 'form-label', style: 'color: blue;', include_blank: true

然后在我的js文件中:

jQuery(function () {
  $('#location_select').css("display","none");
  var locations;
  locations = $('#career_location_id').html();
  return $('#career_region_id').change(function () {
    var options, region;
    region = $('#career_region_id :selected').text();
    options = $(locations).filter("optgroup[label='" + region + "']").html();
    if (options) {
      $('#career_location_id').html(options);
      return $('#location_select').show();
      return $('#career_location_id').parent().show()
      return $('form-select select').css('margin-left', '15px;');
    } else {
      $('#career_location_id').empty();
      return $('#career_location_id').parent().empty();
    }
  });
});

它运作良好,但我的问题与保存其所附的表单有关。因此,我有9个区域,其中一个是“所有区域”。因此,如果他们选择“地区:所有地区”,则没有位置相关联,但是我想要的是将表格应用于所有地区。

这是保存时的控制器操作吗?还是有更好的方法来处理表单中的所有申请?

0 个答案:

没有答案