首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使Carmen-Rails分区选择正确工作

使Carmen-Rails分区选择正确工作
EN

Stack Overflow用户
提问于 2014-09-09 19:59:10
回答 1查看 732关注 0票数 0

我一直在努力让Carmen-rails演示在我的应用程序中工作,但没有效果。我知道coffeescript正在工作,因为我可以使用document.ready来工作。据我所知,目前的问题是,当国家发生变化时,国家根本不更新。这是我所拥有的。

在我的表单中,它使用simple_form:

代码语言:javascript
复制
%h3 Country
= f.input :country, priority: %w(US CA IL), input_html: {id: "country_select"}, prompt: 'Please select a country'
= render partial: 'subregion_select', locals: {parent_region: f.object.country}

_subregion_select部分:

代码语言:javascript
复制
#customer_state_code_wrapper
  - parent_region ||= params[:parent_region]
  - logger.info("Parent region is #{parent_region}")
  - country = Carmen::Country.coded(parent_region) unless parent_region.nil?
  - if country.nil?
    %em Please select a country above
  - elsif country.subregions?
    = subregion_select(:customer, :state, parent_region)
  - else
    = text_field(:customer, :state)

customer.js.coffee

代码语言:javascript
复制
$ ->
  $('select#country_select').change (event) ->
    alert "Blah"
    select_wrapper = $('#customer_state_code_wrapper')
    $('select', select_wrapper).attr('readonly', true)
    country_code = $(this).val()
    url = "/customers/subregion_options?parent_region=#{country_code}"
    select_wrapper.load(url)

当我单击“国家”复选框并更改值时,不会发生任何事情,甚至不会触发警报。对于选择框,我有正确的id,如下所示:

代码语言:javascript
复制
<select class="country optional" id="country_select" name="customer[offices_attributes]

我在java的控制台中没有看到错误,我也不知道自己做错了什么。任何帮助都将不胜感激!

为了更好地格式化,当我在控制台中查看已编译的JS文件时,它显示了以下内容:

代码语言:javascript
复制
(function() {
  $(function() {
    return $('select#country_select').change(function(event) {
      var country_code, select_wrapper, url;
      alert("Blah");
      select_wrapper = $('#customer_state_code_wrapper');
      $('select', select_wrapper).attr('readonly', true);
      country_code = $(this).val();
      url = "/customers/subregion_options?parent_region=" + country_code;
      return select_wrapper.load(url);
    });
  });
}).call(this);

经过进一步研究后,我认为问题可能是该表单是从通过simple_nested_form加载的部分呈现出来的,该部分通过以下方式加载:

= f.fields_for :offices do |o| = render :partial => 'offices', :locals => {:o => o} = f.link_to_add image_tag("layout_add.png") + " Add Another Office", :offices

因此,我认为当加载javascript时,没有任何东西可供它附加。当我单击加载表单的link_to_add时,然后运行:

代码语言:javascript
复制
$('select#country_select').change ->
  alert "blah"

通过控制台,它可以正常工作。那么,在有人单击显示表单的link_to_add之后,如何获得加载脚本呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-25 15:34:53

杰德的最后一句话使我发现了这个问题。我修改了js.coffee以监视选中框中的更改,该复选框正确地触发了事件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25752655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档