我一直在努力让Carmen-rails演示在我的应用程序中工作,但没有效果。我知道coffeescript正在工作,因为我可以使用document.ready来工作。据我所知,目前的问题是,当国家发生变化时,国家根本不更新。这是我所拥有的。
在我的表单中,它使用simple_form:
%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部分:
#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
$ ->
$('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,如下所示:
<select class="country optional" id="country_select" name="customer[offices_attributes]我在java的控制台中没有看到错误,我也不知道自己做错了什么。任何帮助都将不胜感激!
为了更好地格式化,当我在控制台中查看已编译的JS文件时,它显示了以下内容:
(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时,然后运行:
$('select#country_select').change ->
alert "blah"通过控制台,它可以正常工作。那么,在有人单击显示表单的link_to_add之后,如何获得加载脚本呢?
发布于 2014-09-25 15:34:53
杰德的最后一句话使我发现了这个问题。我修改了js.coffee以监视选中框中的更改,该复选框正确地触发了事件。
https://stackoverflow.com/questions/25752655
复制相似问题