此railscast中的coffeescript指定了html选择选项列表的ID:
states = $('#person_state_id').html()
但是,我正在使用嵌套表单,它会动态生成新的表单字段及其ID。例如:
book_extents_attributes_new_extents_extent_value_input
或
book_measurements_attributes_2_measure_type_input
如何动态生成这些ID?
答案 0 :(得分:0)
一位善良的人帮助了我:这是一个解决方案。
对于嵌套表单字段,#view partial,按照railscast。
= javascript_include_tag 'simple_tooltip'
.nested-fields
= f.inputs :class => :data_table_format do
= f.input :client_id, :as => :hidden, :value => current_user.client_id
= f.input :extent_value, :label => "Extent", :input_html => {:class => 'nested_narrow_input'}
%li.select
= f.collection_select :extent_type_id, ExtentUnit.order(:value), :id, :value, {include_blank: true}, {:class => 'nested_drop_down_one_of_two'}
%li.select
= f.grouped_collection_select :extent_unit_id, ExtentUnit.order(:value), :extent_types, :value, :id, :value, {include_blank: true}, {:class => 'nested_drop_down_two_of_two'}
.links
= link_to_remove_association image_tag("#{static_root}/cross.png", {:class => :cross}), f
#application.js或资产管道中的某个地方
$(document).ready(function(){
$(".nested_drop_down_two_of_two").each(function(i,v){
$(this).attr("data-content",$(this).html());
});
$(".nested_drop_down_one_of_two").live("change", function() {
selected_type = $(this).find("option:selected").text();
var content = $($(this).parent("li.select").next().find(".nested_drop_down_two_of_two").attr("data-content")).filter("[label='"+selected_type+"']").html()
$(this).parent("li").next().find(".nested_drop_down_two_of_two").html(content);
return true;
});
});