1

Railscast 88 修订版

此 railscast 中的咖啡脚本指定了 html 选择选项列表的 ID:

  states = $('#person_state_id').html()

但是,我使用的是嵌套表单,它会动态生成新的表单字段及其 ID。例如:

book_extents_attributes_new_extents_extent_value_input

或者

book_measurements_attributes_2_measure_type_input

如何即时生成这些 ID?

4

1 回答 1

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;
        });

    });
于 2012-04-30T17:39:51.440 回答