2

コレクションの選択 (ドロップダウン) ボックスに基づいて表示したい単一のモデルに大量のデータがあります。断面データはこんな感じ・・・

class CreateSections < ActiveRecord::Migration
  def change
    create_table :sections do |t|
      t.string :supervisor
      t.string :catagory
      t.string :title
      t.string :description
      t.string :days
      t.string :start_date
      t.string :end_date
      t.string :start_time
      t.string :end_time
      t.string :course_id
      t.string :room
      t.string :building
      t.string :location
      t.string :tuition
      t.string :lab_fee
      t.string :insurance_fee
      t.string :technology_fee

      t.timestamps
    end
  end
end

私のコレクションの選択は次のようになります...

  collection_select(:section, :section_id, @sections, :id, :title)

そのため、ユーザーはドロップダウンからタイトルを選択し、説明を表示したいと思います。理想的には、jquery でこれを行いたいと考えています。

編集

htmlがどのように見えるかについての詳細は次のとおりです...

 <select id="section_section_id" name="section[section_id]"><option value="1">Long Title</option>
<option value="2"> Alternative Investments for Small Business  NEW</option>
<option value="3">Bookkeeping Basics for the Natural Products Industry  NEW  </option>
...

app/assets/javascripts/sections.js に...

$(function(){
  $("#section_section_id").change(function(){
    alert('wintas');
  })
})

選択したオプションに基づいて、そのオプションに関連付けられた値を表示したいと思います。

4

1 に答える 1

2

options_for_selectそのオプションのデータ属性に表示したい情報を配置するために使用できます

フォームのコンテキストで選択があると仮定します

<%= f.select :section, :section_id, options_for_select(@sections.map{ |s| [s.title, s.id, {'data-description'=>s.description}] }) %>

これは、このようなhtmlを生成します

<option value="2" data-description= "description of 2"> Alternative Investments for Small Business  NEW</option>
<option value="3" data-description="description of 3">Bookkeeping Basics for the Natural Products Industry  NEW  </option>

その後、jsを使用して表示できます

これはjsの動作デモです

http://jsfiddle.net/Tt3TS/

それが役立つことを願っています

于 2013-04-03T15:32:09.707 に答える