Javascript 让Carmen Rails子区域选择正确工作
我一直在努力让Carmen rails演示程序在我的应用程序中运行,但毫无效果。我知道coffeescript正在运行,因为我可以使用document.ready使事情正常进行。从我所能看出的这一点上的问题是,当国家发生变化时,国家根本不会更新。这是我的 在使用简单表格的我的表格中:Javascript 让Carmen Rails子区域选择正确工作,javascript,jquery,ruby-on-rails,coffeescript,Javascript,Jquery,Ruby On Rails,Coffeescript,我一直在努力让Carmen rails演示程序在我的应用程序中运行,但毫无效果。我知道coffeescript正在运行,因为我可以使用document.ready使事情正常进行。从我所能看出的这一点上的问题是,当国家发生变化时,国家根本不会更新。这是我的 在使用简单表格的我的表格中: %h3 Country = f.input :country, priority: %w(US CA IL), input_html: {id: "country_select"}, prompt: 'Please
%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}
_子区域\u选择部分:
#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)
顾客咖啡
$ ->
$('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]
进一步查看后,我认为问题可能在于此表单是从通过简单嵌套表单加载的部分渲染的,通过:
=f.字段| u用于:办公室| o|
=render:partial=>'offices',:locals=>{:o=>o}
=f.link_to_add image_taglayout_add.png+添加另一个办公室,:办公室
因此,我认为当加载javascript时,没有任何东西可供它附加。当我单击链接\u to\u add时,该链接将加载表单,然后运行:
$('select#country_select').change ->
alert "blah"
通过控制台,它可以正常工作。那么,在有人单击显示表单的链接“添加”后,如何加载脚本呢?Jed的最后一条评论让我找到了问题所在。我修改了js.coffee,以观察选择框上的更改,从而正确触发事件。如果在web控制台中进行评估,结果会是什么:$'selectcountry\u select'一旦加载该页面?我使用的是Chrome,当我查看编译的Javascript文件时,输出如下所示:function{$function{return$'selectcountry\u select'.changefunctionevent{var country_code,select_wrapper,url;alertBlah;select_wrapper=$'customer_state_code_wrapper';$'select',select_wrapper.attr'readonly',true;country_code=$this.val;url=/customers/subsection_options?parent_region=+country_code;返回select_wrapper.loadurl;};};};}.callthis;我不确定如何评估JSI,如果我在返回的控制台中键入$'selectcountry\u select':请选择一个国家。这还包括选择框的所有元素,但由于长度原因,我遗漏了它们。删除select事件中除您的警报之外的所有代码,然后查看它是否会发出警报。我清除了所有内容除了警报,还是什么都没有发生。我也遇到了同样的问题。你能发布你的解决方案吗?基本上,我的表单是嵌套在主表单中的,函数的属性是嵌套的。我最后要做的是在它自己的页面中将其分离出来,而不是在主表单中使用嵌套的属性。这解决了问题。
$('select#country_select').change ->
alert "blah"